繁体   English   中英

Android简单的百分比布局?

[英]Android simple percentage layout?

有没有办法在Android上使用元素将其全屏显示? 我尝试使用表格,GridView和线性控件来完成此操作,但是由于很长时间以来我一直在为此而苦苦挣扎,并且找不到帮助我的帖子。

我只是对自己想做的事情做了一小幅画: http : //i.imgur.com/W0oTyaC.png 在此处输入图片说明

简而言之:

  • 标签和按钮应与其内容一样大,并适合列中最大的标签

  • 该按钮应贴在右侧

  • 标签应贴在左侧

  • 文本框应只填满标签/按钮之间的剩余空间,但如果内容较大,则不应“弹出”右侧的按钮...

我最后的尝试是GridView,它看起来是可以接受的,但实际上不是。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add nutrition part"
                android:id="@+id/textView"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

            <TextView
                android:text="Selected part"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView15"
                android:layout_column="0"
                android:layout_row="1"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:text=""
                android:enabled="false"
                android:id="@+id/dialogMakeMealPartNutrition"
                android:layout_column="1"
                android:layout_row="1"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1"
                android:layout_gravity="fill_horizontal" />

            <Button
                android:text="Find nutrition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buttonMakeMealPartFindNutrition"
                android:layout_column="2"
                android:layout_row="1"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

            <TextView
                android:text="Gram"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView17"
                android:layout_column="0"
                android:layout_row="2"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:id="@+id/dialogMakeMealPartGram"
                android:layout_column="1"
                android:layout_row="2"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1"
                android:layout_gravity="fill_horizontal" />

            <Button
                android:text="Abort"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dialogMakeMealPartButtonAbort"
                android:layout_column="0"
                android:layout_row="3"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

            <Button
                android:text="Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dialogMakeMealPartButtonAdd"
                android:layout_column="1"
                android:layout_row="3"
                android:layout_columnSpan="1"
                android:layout_rowSpan="1" />

        </GridLayout>

    </ScrollView>

</RelativeLayout>

编辑:

ConstraintLayout对于我的目的做得很好-感谢Luca的良好暗示:

任务描述

现在的代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FFFFFF">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Add nutrition part"
                android:id="@+id/headline"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:text="Selected part"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView15"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/buttonMakeMealPartFindNutrition" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:text=""
                android:enabled="false"
                android:id="@+id/dialogMakeMealPartNutrition"
                app:layout_constraintStart_toEndOf="@+id/textView15"
                app:layout_constraintEnd_toStartOf="@+id/buttonMakeMealPartFindNutrition"
                app:layout_constraintTop_toTopOf="@+id/buttonMakeMealPartFindNutrition" />

            <Button
                android:text="Find nutrition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buttonMakeMealPartFindNutrition"
                app:layout_constraintTop_toBottomOf="@+id/headline"
                app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartNutrition"
                app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                android:text="Gram"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView17"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartNutrition" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:id="@+id/dialogMakeMealPartGram"
                app:layout_constraintStart_toStartOf="@+id/dialogMakeMealPartNutrition"
                app:layout_constraintEnd_toEndOf="@+id/dialogMakeMealPartNutrition"
                app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartNutrition" />

            <Button
                android:text="Abort"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dialogMakeMealPartButtonAbort"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartGram" />

            <Button
                android:text="Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dialogMakeMealPartButtonAdd"
                app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartButtonAbort"
                app:layout_constraintTop_toBottomOf="@+id/dialogMakeMealPartGram" />

        </android.support.constraint.ConstraintLayout>

    </ScrollView>

</RelativeLayout>

您可以使用ConstraintLayout来实现此目的, 在这里可以找到它的解释。

您的布局文件应如下所示:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Add nutrition part"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Selected part"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"/>

    <EditText
        android:id="@+id/dialogMakeMealPartNutrition"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:inputType="text"
        app:layout_constraintStart_toEndOf="@+id/textView15"
        app:layout_constraintEnd_toStartOf="@+id/buttonMakeMealPartFindNutrition"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:text=""/>

    <Button
        android:id="@+id/buttonMakeMealPartFindNutrition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/dialogMakeMealPartNutrition"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Find nutrition"/>

    ...

</android.support.constraint.ConstraintLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM