Android 多屏布局支持不起作用
Android multi-screen layout support doesn't work
我试图在我的 android 应用程序上实现多屏支持,因此我提供了几个布局文件夹 "layout-sw480dp"、"layout-sw600dp"、"layout-sw720dp" 和同名的 xml 文件。我使用 720dp 布局作为我的主要布局,在 10.1" 平板电脑上一切看起来都不错,但 4.3" phone 无法加载相应的布局。我已经阅读了大量文章和不同的问题,但仍然无法找到解决方案。
谁能帮我解决这个问题?
默认 xml 布局示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blackboard"
android:orientation="vertical" >
<TextView
android:id="@+id/welcomeTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:text="@string/welcomeLabel"
android:textColor="@color/white"
android:textSize="56sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="55dp"
android:layout_marginTop="30dp"
android:weightSum="100" >
<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/nameLabel"
android:textColor="@color/white"
android:textSize="26sp" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="20"
android:background="@color/white"
android:textColor="@color/blue"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="55dp"
android:paddingTop="20dp"
android:weightSum="100" >
<TextView
android:id="@+id/eqNumberLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/eqNumberLabel"
android:textColor="@color/white"
android:textSize="26sp" />
<EditText
android:id="@+id/eqNumberEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="10"
android:background="@color/white"
android:inputType="number"
android:textColor="@color/blue"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<CheckBox
android:id="@+id/reducedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:layout_marginRight="10dp"
android:button="@drawable/checkbox"
android:layout_gravity="center" >
</CheckBox>
<TextView
android:id="@+id/reducedLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reducedLabel"
android:textColor="@color/white"
android:textSize="36sp" />
</LinearLayout>
<Button
android:background="@drawable/begin_button"
android:layout_marginTop="20dp"
android:id="@+id/beginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="false"
android:text="@string/beginButton"
android:textColor="@color/black"
android:textSize="36sp"/>
我遇到了同样的问题。现在我只是在 onCreate、onConfigurationChanged 和其他时间在代码中调整我的布局。这适用于任何尺寸的屏幕。下面的代码为设备提供了实际像素。你能提供默认值 xml 吗?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final float width = dm.widthPixels;
final float height = dm.heightPixels;
根据平板电脑的大小和分辨率将您的平板电脑资源保存在不同的文件夹中:
drawable-large : 用于 7' 平板电脑
drawable-xlarge:适用于 10' 平板电脑
要支持手机,请使用以下文件夹:
可绘制的mdpi
可绘制-hdpi
绘制-xhdpi
drawable-xxhdpi
P.S: 屏幕尺寸和分辨率不同。
我试图在我的 android 应用程序上实现多屏支持,因此我提供了几个布局文件夹 "layout-sw480dp"、"layout-sw600dp"、"layout-sw720dp" 和同名的 xml 文件。我使用 720dp 布局作为我的主要布局,在 10.1" 平板电脑上一切看起来都不错,但 4.3" phone 无法加载相应的布局。我已经阅读了大量文章和不同的问题,但仍然无法找到解决方案。 谁能帮我解决这个问题?
默认 xml 布局示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blackboard"
android:orientation="vertical" >
<TextView
android:id="@+id/welcomeTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:text="@string/welcomeLabel"
android:textColor="@color/white"
android:textSize="56sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="55dp"
android:layout_marginTop="30dp"
android:weightSum="100" >
<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/nameLabel"
android:textColor="@color/white"
android:textSize="26sp" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="20"
android:background="@color/white"
android:textColor="@color/blue"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="55dp"
android:paddingTop="20dp"
android:weightSum="100" >
<TextView
android:id="@+id/eqNumberLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@string/eqNumberLabel"
android:textColor="@color/white"
android:textSize="26sp" />
<EditText
android:id="@+id/eqNumberEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="10"
android:background="@color/white"
android:inputType="number"
android:textColor="@color/blue"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<CheckBox
android:id="@+id/reducedCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:layout_marginRight="10dp"
android:button="@drawable/checkbox"
android:layout_gravity="center" >
</CheckBox>
<TextView
android:id="@+id/reducedLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reducedLabel"
android:textColor="@color/white"
android:textSize="36sp" />
</LinearLayout>
<Button
android:background="@drawable/begin_button"
android:layout_marginTop="20dp"
android:id="@+id/beginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="false"
android:text="@string/beginButton"
android:textColor="@color/black"
android:textSize="36sp"/>
我遇到了同样的问题。现在我只是在 onCreate、onConfigurationChanged 和其他时间在代码中调整我的布局。这适用于任何尺寸的屏幕。下面的代码为设备提供了实际像素。你能提供默认值 xml 吗?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final float width = dm.widthPixels;
final float height = dm.heightPixels;
根据平板电脑的大小和分辨率将您的平板电脑资源保存在不同的文件夹中: drawable-large : 用于 7' 平板电脑 drawable-xlarge:适用于 10' 平板电脑 要支持手机,请使用以下文件夹: 可绘制的mdpi 可绘制-hdpi 绘制-xhdpi drawable-xxhdpi
P.S: 屏幕尺寸和分辨率不同。