繁体   English   中英

Android布局:两个固定宽度视图之间的可变宽度视图

[英]Android layout: variable-width view between two fixed-width views

一个如何水平放置3个视图,以使外部2个视图具有固定的宽度,中间的视图填充剩余的可用水平空间? 我所有的尝试都导致最右边的视图被推离屏幕。

为了解决这个问题,我努力了一段时间,并找到了如何使用RelativeLayout做到这一点。 请参见下面的代码示例,并密切注意layout_toRightOflayout_toLeftOflayout_alignParentRight

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/eventDetailSectionHeight"
    android:background="@color/grayout">

    <SomeFixedWidthView
        android:id="@+id/leftFixedWidthView"
        android:layout_width="100dp"
        android:layout_height="match_parent"/>

    <SomeVariableWidthView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/leftFixedWidthView"
        android:layout_toLeftOf="@+id/rightFixedWidthView"/>

    <SomeFixedWidthView
        android:id="@+id/rightFixedWidthView"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

尝试根据要求为每个孩子设置layout_weight。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


        <View
            android:id="@+id/leftView"
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="match_parent"
            android:background="#ff0000"/>

        <View
            android:id="@+id/middleView"
            android:layout_width="0dp"
            android:layout_weight="0.2"
            android:layout_height="match_parent"
            android:background="#33cc33"/>

        <View
            android:id="@+id/rightView"
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="match_parent"
            android:background="#ff0000"/>/>


</LinearLayout>

将以下代码用于您想要的结果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/leftView"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:background="#ff0000"/>

    <View
        android:id="@+id/middleView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#33cc33"/>

    <View
        android:id="@+id/rightView"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:background="#ff0000"/>

</LinearLayout>

暂无
暂无

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

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