【问题标题】:How to fit my layout to screen using xml, grid layout / linear layout does not work如何使用 xml 使我的布局适合屏幕,网格布局/线性布局不起作用
【发布时间】:2018-03-15 14:16:50
【问题描述】:

我必须制作这种布局,但我不知道如何使用 xml 代码使其适合屏幕。有一种方法可以使用 java 代码并获取屏幕分辨率,然后设置所有组件的宽度和高度,但我认为这不是一个好主意。使用网格布局你不能适应元素,使用线性你不能像这样在一行中做几个。我也尝试使用一些线性布局,但在这种情况下,它不可能像我想要的那样定位元素。你猜猜怎么解决?!

如何显示这种类型的自定义网格组件

图片:http://i.stack.imgur.com/mFCFX.png

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    您应该使用多个嵌套的相对布局,并使用 layout_weight 属性来适当地调整它们的大小,并使用 layout_below、layout_toRightOf 等来将它们定位到您想要的位置。

    here

    【讨论】:

    • @Charliee 如果我的回答对您有帮助,最好接受它,点击旁边的复选标记 :-)
    【解决方案2】:

    更新:我们知道,API 级别 26 已弃用百分比支持库。ConstraintLayout 是实现相同扁平 xml 结构的新方法。

    Updated Github Project

    更新示例:

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/fifty_thirty"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff8800"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            android:textSize="25sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff5566"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintLeft_toRightOf="@id/fifty_thirty"
            app:layout_constraintTop_toBottomOf="@id/fifty_thirty"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    已弃用

    不用担心您的布局可以通过 android 的百分比支持库满足您的任何要求。

    Demo HERE !

    GitHub project HERE !

    考虑这个简单的演示示例。

    <android.support.percent.PercentRelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/fifty_huntv"
            android:background="#ff7acfff"
            android:text="20% - 50%"
            android:textColor="@android:color/white"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="50%" />
        <TextView
            android:layout_toRightOf="@id/fifty_huntv"
            android:background="#ffff5566"
            android:text="80%-50%"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_heightPercent="80%"
            app:layout_widthPercent="50%"
            />
    </android.support.percent.PercentRelativeLayout>
    

    真的太棒了!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2021-09-15
      • 2017-07-05
      • 2014-02-25
      • 2021-06-21
      • 1970-01-01
      • 2014-06-07
      相关资源
      最近更新 更多