Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果一个字段具有灵活的宽度,而另一个字段具有设置的宽度,如何强制布局?_Android_Android Layout - Fatal编程技术网

Android 如果一个字段具有灵活的宽度,而另一个字段具有设置的宽度,如何强制布局?

Android 如果一个字段具有灵活的宽度,而另一个字段具有设置的宽度,如何强制布局?,android,android-layout,Android,Android Layout,我正在尝试对齐一行中的三个文本字段。第一个字段(bonusCode)中的字符永远不会超过8个,第三个字段(state)总是2个字符,中间的一个字段(city)是我要扩展的字段。但我似乎不知道该怎么做。以下是它当前的外观: 正如您在屏幕截图中所看到的,随着布局大小的改变,城市和州之间的差距会增大。我希望它们之间的距离相同。扩展应该在bonusCode和City之间 以下是我的单元格XML: <RelativeLayout xmlns:android="http://schemas

我正在尝试对齐一行中的三个文本字段。第一个字段(bonusCode)中的字符永远不会超过8个,第三个字段(state)总是2个字符,中间的一个字段(city)是我要扩展的字段。但我似乎不知道该怎么做。以下是它当前的外观:

正如您在屏幕截图中所看到的,随着布局大小的改变,城市和州之间的差距会增大。我希望它们之间的距离相同。扩展应该在bonusCode和City之间

以下是我的单元格XML:

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/bonusListImage"
        android:contentDescription="@string/mainImageDescription"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="8dp"
        android:src="@drawable/no_image_taken" />

    <TextView
        android:id="@+id/bonusListName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@id/bonusListImage"
        android:text="@string/valueBonusName" />

    <TextView
        android:id="@+id/bonusListCategory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bonusListName"
        android:layout_toEndOf="@id/bonusListImage"
        android:text="@string/valueCategory" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:orientation="horizontal"
        android:layout_below="@+id/bonusListCategory"
        android:layout_toEndOf="@id/bonusListImage">

        <TextView
            android:id="@+id/bonusListCode"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:text="@string/valueBonusCode" />

        <TextView
            android:id="@+id/bonusListCity"
            android:layout_width="0dp"
            android:layout_weight="12"
            android:layout_height="wrap_content"
            android:textAlignment="textEnd"
            android:text="@string/valueBonusCity"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/bonusListState"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textAlignment="textEnd"
            android:layout_marginStart="4dp"
            android:text="@string/valueBonusState"
            android:textStyle="bold" />

    </LinearLayout>
</RelativeLayout>


您可以使用相对布局。对于状态视图,请保持“layout\u alignParentRight”为True。它将使此视图的右边缘与父视图的右边缘匹配。同时将城市字段与州字段对齐,对于城市字段,请将布局保持在州视图的左视图。你可以在两者之间保持填充

您已将权重设置为线性布局内的所有textview,该布局根据权重划分视图。将
layout\u weight
设置为仅bonusListCode文本视图,并从state和city中删除,它将按照您想要的方式工作

试试这个:

<RelativeLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<ImageView
    android:id="@+id/bonusListImage"
    android:contentDescription="@string/mainImageDescription"
    android:layout_width="64dp"
    android:layout_height="48dp"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="8dp"
    android:src="@drawable/no_image_taken" />

<TextView
    android:id="@+id/bonusListName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@id/bonusListImage"
    android:text="@string/valueBonusName" />

<TextView
    android:id="@+id/bonusListCategory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/bonusListName"
    android:layout_toEndOf="@id/bonusListImage"
    android:text="@string/valueCategory" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:layout_below="@+id/bonusListCategory"
    android:layout_toEndOf="@id/bonusListImage">

    <TextView
        android:id="@+id/bonusListCode"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/valueBonusCode" />

    <TextView
        android:id="@+id/bonusListCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="textEnd"
        android:text="@string/valueBonusCity"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/bonusListState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="textEnd"
        android:layout_marginStart="4dp"
        android:text="@string/valueBonusState"
        android:textStyle="bold" />

</LinearLayout>


这起作用了。从技术上讲,这是否意味着bonusCode字段会扩展大小,因为它是一个有重量的字段?(显然这个用例没有问题,但对未来的应用程序很好奇)。@DJFriar,检查一下这个描述,它将对你的未来有所帮助。