如何使用 xml 使我的布局适合屏幕,网格布局/线性布局不起作用

How to fit my layout to screen using xml, grid layout / linear layout does not work

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

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

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

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

here

更新: 正如我们所知,百分比支持库已从 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>

真的很棒!!!