如何根据 android 中的屏幕尺寸自动调整 margin_left 和 margin_right 尺寸
How to auto resize the margin_left and margin_right sizes based on screen size in android
1>在所有不同的屏幕尺寸下,我如何看到这样的屏幕?
2> 为此我必须遵循哪些步骤?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sridhar.textviewautosize.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="36dp"
android:layout_marginTop="25dp"
android:autoSizeTextType="uniform"
android:text="Hello World I am Checking AutoFill Text View is working or not for different screens" />
</LinearLayout>
请检查此代码并告诉我如何根据小型、中型、大型和超大型屏幕尺寸自动调整左边距和右边距属性的大小。
您应该在不同的 values
文件夹中定义边距,如下所示:
如果您需要支持更多屏幕尺寸,您可以根据需要添加任意数量的 sw
文件夹。您可以根据下图计算 sw dp
:
-> nexus 4: 768/2 = 384dp, Nexus 5: 1080/3 =360, Pixel 2: 1080 * 160/420 = 410dp.
您需要设置hdpi、xhdpi等。它将涵盖所有 phone 尺寸。设置 dimensions.xml 内部资源。
例如:
res/values-hdpi/dimensions.xml, res/values-xhdpi/dimensions.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">10dp</dimen>
<dimen name="activity_vertical_margin">20dp</dimen>
使用此方法获取屏幕尺寸
public void get_screen_size(){
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
Toast.makeText(this, "Extra Large Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small Screen", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is not xlarge, large, normal or small", Toast.LENGTH_LONG).show();
}
}
然后根据实际屏幕尺寸,应用您认为需要的边距以编程方式。
但是,我建议您避免同时使用边距,而是寻找另一个可能使用权重的解决方案,以便它可以在任何屏幕上工作。
最好的解决方案当然是遵循 android 建议的支持不同屏幕尺寸的建议,可以在此处找到:
https://developer.android.com/guide/practices/screens_support.html
https://developer.android.com/training/multiscreen/screensizes.html
1>在所有不同的屏幕尺寸下,我如何看到这样的屏幕?
2> 为此我必须遵循哪些步骤?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sridhar.textviewautosize.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="36dp"
android:layout_marginTop="25dp"
android:autoSizeTextType="uniform"
android:text="Hello World I am Checking AutoFill Text View is working or not for different screens" />
</LinearLayout>
请检查此代码并告诉我如何根据小型、中型、大型和超大型屏幕尺寸自动调整左边距和右边距属性的大小。
您应该在不同的 values
文件夹中定义边距,如下所示:
如果您需要支持更多屏幕尺寸,您可以根据需要添加任意数量的 sw
文件夹。您可以根据下图计算 sw dp
:
-> nexus 4: 768/2 = 384dp, Nexus 5: 1080/3 =360, Pixel 2: 1080 * 160/420 = 410dp.
您需要设置hdpi、xhdpi等。它将涵盖所有 phone 尺寸。设置 dimensions.xml 内部资源。
例如: res/values-hdpi/dimensions.xml, res/values-xhdpi/dimensions.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">10dp</dimen>
<dimen name="activity_vertical_margin">20dp</dimen>
使用此方法获取屏幕尺寸
public void get_screen_size(){
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
Toast.makeText(this, "Extra Large Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
Toast.makeText(this, "Large Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
Toast.makeText(this, "Normal Screen", Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
Toast.makeText(this, "Small Screen", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is not xlarge, large, normal or small", Toast.LENGTH_LONG).show();
}
}
然后根据实际屏幕尺寸,应用您认为需要的边距以编程方式。
但是,我建议您避免同时使用边距,而是寻找另一个可能使用权重的解决方案,以便它可以在任何屏幕上工作。
最好的解决方案当然是遵循 android 建议的支持不同屏幕尺寸的建议,可以在此处找到:
https://developer.android.com/guide/practices/screens_support.html
https://developer.android.com/training/multiscreen/screensizes.html