【问题标题】:Support different type of the screen programmatically以编程方式支持不同类型的屏幕
【发布时间】:2018-07-06 17:23:35
【问题描述】:

我的布局如何在不使用约束布局和使用 layout-small、layout-large、layout-normal 和 layout-xlarge 等文件夹的情况下支持不同的屏幕。我不想使用它们,因为我确实有很多布局。请帮我。 这是我的布局之一:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/job_order_no"
        android:textSize="15sp"
        android:textColor="#000"
        android:fontFamily="@font/roboto_bold"
        android:layout_weight="1"
        android:text ="A"
        />
</LinearLayout>

【问题讨论】:

标签: android android-layout


【解决方案1】:

尝试使用带有 weightSum 和权重的 LinearLayout。 下面是在屏幕上平均划分 2 个按钮的示例,

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 2"/>

</LinearLayout>

您可以根据需要更改 weightSum 和 weights。 只需确保所有权重的总和必须等于 weightSum。

希望对你有帮助

【讨论】:

    【解决方案2】:

    为此,您需要在 res 目录中创建 2 个文件夹,例如 values-sw600dp(兼容 8 英寸选项卡)和 values-sw720dp(兼容 10 英寸选项卡)。 在 values-sw600dp 中,创建 dimen.xml 并根据您的要求在该文件中指定尺寸。(对于 8 英寸选项卡)。 在 values-sw720dp 中,创建 dimen.xml 并根据您的要求在该文件中指定尺寸。(对于 10 英寸标签)

    例如:

        <dimen name="txt_title">20sp</dimen>  (in values-sw600dp)
        <dimen name="txt_title">24sp</dimen>  (in values-sw720dp)
    

    并在您的布局中使用它在维度上方,如下所示:

        android:textSize="@dimen/txt_title"
    

    您还可以使用此设置视图的高度和宽度。

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多