0

我意识到这已经被问过无数次了,但我还没有为自己想出一个解决方案。我想制作一组简单的按钮,布局如下,而不使用 GridLayout。我对 TableLayout 或 RelativeLayout 也不太满意。对我有用的是LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="5dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#d0b0b0"
            android:paddingRight="10dp"
            android:textSize="15dip" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp"
            android:background="#808080" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#a09a09"
            android:textSize="15dip" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="5dp" >

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#456456"
            android:padding="10dp"
            android:textSize="15dip" />
    </LinearLayout>

</LinearLayout>

但我收到有关“嵌套权重不利于性能”的警告。真的吗?这么简单的布局?我可以忽略警告吗?还有其他(优雅的?)方法可以做到这一点吗?

线性布局,3 个按钮

4

3 回答 3

3

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

更新了 Github 项目

更新示例:

<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 支持库获得了很棒的解决方案,可以将我们的布局平均划分为百分比。

避免完全混乱LinearLayout weights

compile 'com.android.support:percent:23.0.0'

代码和概念

Github 项目

考虑这个简单的布局。

百分比布局演示

<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:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff7acfff"
        android:text="20% - 50%"
        android:textColor="@android:color/white"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/fifty_huntv"
        android:background="#ffff5566"
        android:text="80%-50%"
        app:layout_heightPercent="80%"
        app:layout_widthPercent="50%"
        />

</android.support.percent.PercentRelativeLayout>

宾果游戏我们完成了。真的很棒:-)

于 2015-09-06T08:06:00.590 回答
1

您可以通过对当前布局文件进行一些调整来避免嵌套布局警告:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <View
        android:id="@+id/anchor"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_centerVertical="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/anchor"
        android:layout_alignParentTop="true"
        android:padding="5dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#d0b0b0"
            android:paddingRight="10dp"            
            android:textSize="15dip" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp"
            android:background="#808080" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#a09a09"            
            android:textSize="15dip" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/anchor"
        android:orientation="horizontal"
        android:padding="5dp" >

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#456456"
            android:padding="10dp"            
            android:textSize="15dip" />
        <!-- textSize should be set in sp units, like 15sp -->
    </LinearLayout>

</RelativeLayout> 

此外,您可能希望删除LinearLayout包裹单曲的Button(并在 中添加 5dp 边距Button)。

于 2012-07-28T04:56:58.150 回答
-1

另一个关于 android 布局的有趣细节,它还具有删除嵌套权重上的 lint 警告的效果。如果我将按钮放入合并文件中:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#d0b0b0"
        android:textSize="15dip" />

</merge>

.. 对其他按钮(以及我制作的间隔视图)执行相同操作,然后将这些合并 xml 文件包含到主 xml 文件中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="5dp" >

        <include layout="@layout/button1" />

        <include layout="@layout/spacer" />

        <include layout="@layout/button2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="5dp" >

        <include layout="@layout/button3" />
    </LinearLayout>

</LinearLayout>

..我失去了所有的皮棉警告!lint 只是错过了问题,还是以这种方式更容易膨胀?

它不仅让阅读变得更容易(对我来说),而且给了我很多重用的可能性。我 <3 的 XML 布局!

于 2012-08-03T17:36:08.360 回答