【问题标题】:Android supporting different screen sizeAndroid支持不同的屏幕尺寸
【发布时间】:2018-04-05 19:32:42
【问题描述】:

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

布局小

布局正常

布局-土地

布局-土地-小

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

【问题讨论】:

  • 有这个库可以根据屏幕分辨率自动调整定义的视图大小,您可以尝试一下。 github.com/intuit/sdp
  • 在运行时调整你的布局组件。
  • @Ankit 你能举个例子吗?
  • @Arshad 我正在使用 Eclipse,您提到的链接是工作室的。那么我在 Eclipse 中也必须这样做吗?

标签: android android-layout screen-size


【解决方案1】:

无需创建单独的布局目录

你可以参考这个:

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

注意: Margin 、 Padding 、 Height 、 Width 等可以与sdp 一起使用以获得最佳效果。

在您的 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>

为了字体大小的一致性: 我会推荐你​​使用sp 而不是其他的。

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

               <TextView
                    android:id="@+id/tvFbShare"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="@dimen/_6sdp"             // SDP used
                    android:text="@string/str_share_on_facebook"
                    android:textColor="@color/fb_blue"
                    android:textSize="14sp"                   // SP used />

【讨论】:

  • 即使是按钮、图像、webview 我也可以使用 sp 并且所有屏幕尺寸都一样?我正在使用 Eclipse。
  • 对于按钮、图片、webview,你可以使用“SDP”,而在你想应用textSize的地方,你可以使用“SP”,试试吧,你会得到你想要的结果
  • 当然我会尝试让你知道。谢谢:)
  • 再次感谢。我会试试的。
  • 当然!如果需要更多输入,请告诉我
【解决方案2】:

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

【讨论】:

  • 对于横向/纵向我必须创建单独的布局?是你的意思吗?
【解决方案3】:

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

【讨论】:

  • 所以为了安排按钮或文本视图或图像,我是硬编码。
  • 好的。谢谢,我会尽力让你知道:)
【解决方案4】:

首先在你的 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 更改为您所需的尺寸

【讨论】:

  • 谢谢。我会尽力让你知道。
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多