Android支持不同的屏幕尺寸

时间:2018-04-04 07:41:04

标签: android android-layout screen-size

我找到了很多解决方案,但对我来说没有任何作用。我正在创建一个语言学习应用程序。因此,我使用的是图像视图,按钮,文本视图和网页视图。将有超过15个布局文件。我发现的解决方案似乎必须创建如下的布局。

布局小

layout-normal

layout -land

布局 - 陆地 - 小

所以我必须为这4个文件夹修改这15个布局文件。由于我的文件数量较高,因此编码很困难。有没有其他方法可以做而不是修改文件?如果是这样,请帮助我。或者建议我如何处理大量文件。

4 个答案:

答案 0 :(得分:1)

  

无需创建单独的布局目录

您可以参考:

有关布局的一致性参考和使用(示例如下): https://github.com/intuit/sdp

  

注意:边距,填充,高度,宽度等可与sdp一起使用,以获得最佳效果。

<{1}} gradle

中的

implementation 'com.intuit.sdp:sdp-android:1.0.5'

对于字体大小一致性 我建议您使用<LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/_200sdp" android:orientation="vertical" android:padding="@dimen/_4sdp"> <Button android:id="@+id/btnAdd" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:layout_marginBottom="@dimen/_12sdp" // SDP used android:layout_marginEnd="@dimen/_12sdp" // SDP used android:layout_marginStart="@dimen/_12sdp" // SDP used android:background="@drawable/shape_button_background" android:foreground="?android:selectableItemBackground" android:textColor="@color/white" android:textSize="18sp" // SP used android:text="@string/str_confirm_place_order"/> </LinearLayout> 代替其他人。

  

注意: Button,EditText,TextView等中的textSize可与sp一起使用,以获得最佳效果。

sp

答案 1 :(得分:0)

您还可以使用values / dimens.xml定义不同屏幕尺寸的尺寸。然后,您只需要在布局文件中使用这些尺寸。至于横向/纵向,您可能需要处理2个布局文件。

答案 2 :(得分:0)

无需使用4个文件夹,每个布局最佳解决方案使用sdp

答案 3 :(得分:-1)

首先在Utility类中定义这两个方法

// Method is used to get the height of the screen
public static int getScreenHeight(Context context) {

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    assert wm != null;
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.heightPixels;
}

// Method is used to get the width of the screen
public static int getScreenWidth(Context context) {

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    assert wm != null;
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.widthPixels;
}

使用以上两种方法在 onCreate()

中获取屏幕的高度和宽度
int height = Utility.getScreenHeight(this);
int width = Utility.getScreenWidth(this);

现在,您可以在 onResume()方法中使用这些维度,例如

your_ui_component.getLayoutParams().height = (int) (height * .25);
your_ui_component.getLayoutParams().width = (int) (height * .25);

使用所需尺寸更改.25