首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android布局:两个固定宽度视图之间的可变宽度视图

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

Stack Overflow用户
提问于 2017-02-15 22:40:39
回答 3查看 397关注 0票数 0

如何水平布局3个视图,使外部的2个视图具有固定的宽度,而中间的视图填充剩余的水平空间?我所有这样做的尝试都导致最右边的视图被推到屏幕上。

EN

回答 3

Stack Overflow用户

发布于 2017-02-15 22:40:39

我挣扎了一段时间才让它正常工作,并找到了如何使用RelativeLayout做到这一点。请参阅下面的代码示例,并密切关注layout_toRightOflayout_toLeftOflayout_alignParentRight的用法

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-02-15 23:53:30

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

代码语言:javascript
复制
<?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>
票数 0
EN

Stack Overflow用户

发布于 2017-02-16 00:27:47

使用下面的代码来获得您想要的结果

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42252407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档