【问题标题】:How to create a compatible layout for every screen size?如何为每个屏幕尺寸创建兼容的布局?
【发布时间】:2017-03-09 11:38:30
【问题描述】:

我正在尝试创建一个应用程序...但是当我在各种设备上测试我的应用程序时,该应用程序没有以全屏/相同的分辨率显示...

【问题讨论】:

标签: android android-layout android-studio android-activity


【解决方案1】:

您可以使用ConstaintLayout 来制作响应式用户界面,您可以按照以下官方文档进行操作 https://developer.android.com/training/constraint-layout/index.html

另一个选项是PercentRelativeLayout https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

对于 3*3 矩阵,您可以使用 Tablelayout 类似的东西

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow>
            <TextView
                android:text="first" />
            <TextView
                android:text="second"
                android:gravity="center" />
            <TextView
                android:text="third"
                android:gravity="right" />
        </TableRow>

        <TableRow>
            <TextView
                android:text="first" />
            <TextView
                android:text="second"
                android:gravity="center" />
            <TextView
                android:text="third"
                android:gravity="right" />
        </TableRow>

        <TableRow>
            <TextView
                android:text="first" />
            <TextView
                android:text="second"
                android:gravity="center" />
            <TextView
                android:text="third"
                android:gravity="right"/>
        </TableRow>
    </TableLayout>

或者您可以使用linearlayoutsweightSumlayout_weight 的组合

【讨论】:

  • 你能发给我一个有 3*3 矩阵类型按钮的主要活动布局 xml 吗?
  • 最好自己搜索和探索事物,如果您仍然无法找到更好的方法,请分享您的代码并要求改进。快乐编码:)
  • 查看我的编辑。希望它能帮助你理解这个想法。
  • 我正在以 3*3 矩阵形式添加按钮(9 个按钮)。具有一些宽度和高度,但是当我尝试在不同屏幕尺寸的不同设备上运行应用程序时,我看到按钮是不适应他们离开屏幕或缩小的屏幕尺寸
【解决方案2】:

我正在以 3*3 矩阵形式添加按钮(9 个按钮)。具有一些宽度和高度,但是当我尝试在不同屏幕尺寸的不同设备上运行应用程序时,我看到按钮没有调整到屏幕尺寸他们离开了屏幕或缩小了

【讨论】:

    【解决方案3】:

    将此库用于dp,您无需为每个分辨率设置不同的布局https://github.com/intuit/sdp

    dependencies {
      compile 'com.intuit.sdp:sdp-android:1.0.4'
    }
    

    例子

    <LinearLayout
            android:id="@+id/give_us_a_review_landmine_main_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="@dimen/_27sdp"
            android:paddingLeft="@dimen/_43sdp"
            android:paddingRight="@dimen/_43sdp"
            android:paddingTop="@dimen/_50sdp" />
    

    【讨论】:

    • 你是怎么查的,能解释一下还是放代码?
    • 首先我去gradle而不是写代码而不是点击按钮syn现在它显示错误
    • 错误?出现什么错误?,请检查更新的答案或链接包含 git repo 中的示例。
    • 先生,我的主要问题是我要添加 9 个具有一定大小和背景的按钮,但是当我在不同设备上运行应用程序时,按钮会超出屏幕或缩小...我不知道怎么办
    【解决方案4】:

    使用 Junaid 建议的约束布局,否则正确使用 weightsum。

    【讨论】:

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