简体   繁体   English

LinearLayout填充背景的百分比

[英]LinearLayout Fill Percent Of Background

I am trying to fill a certain percent of a (horizontal) item's background. 我试图填充(水平)项目背景的一定百分比。

I have tried this as many people have recommended: 我尝试了很多人推荐的方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:orientation="horizontal"
    android:weightSum="1"
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/fill_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:background="#000"
        android:orientation="horizontal">
    </LinearLayout>

</LinearLayout>

But the problem is that I have many more layout's below that and this layout end's up pushing the rest to the right like so: 但是问题是我在其下还有更多的布局,而布局的最后将其余部分推向右侧,如下所示: 在此处输入图片说明

How do I make this new LinearLayout background layout go behind the rest of the layouts? 如何使这种新的LinearLayout背景布局落后于其余布局? Here is my full xml: 这是我的完整xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:background="@color/optionItemBackgroundColor"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?selectableItemBackground"
    android:orientation="horizontal"
    android:weightSum="100"
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/fill_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:background="@color/percent_fill_color"
        android:orientation="horizontal">
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="?listPreferredItemHeight"
        android:orientation="vertical"
        android:weightSum="3">

        <TextView
            android:id="@+id/percentText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/activity_vertical_margin"
            android:layout_weight="2"
            android:gravity="center"
            android:textColor="@color/optionItemTextColor"
            android:textSize="@dimen/option_text_size"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/votesText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/activity_vertical_margin"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/optionItemTextColor"
            android:textSize="@dimen/num_votes_text_size"
            android:textStyle="normal" />
    </LinearLayout>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/activity_vertical_margin"
        android:gravity="center_vertical"
        android:minHeight="?listPreferredItemHeight"
        android:textColor="@color/optionItemTextColor"
        android:textSize="@dimen/option_text_size" />

</LinearLayout>

If you are trying to achieve like this try this code 如果您要尝试实现这样的代码,请尝试以下代码 图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#e11f1f"
    android:orientation="vertical"
    android:weightSum="1"
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/fill_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5"
        android:background="#000"
        android:orientation="horizontal">
    </LinearLayout>

</LinearLayout>

if your trying to to get this view try this 如果您想获得此视图,请尝试此 image2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:orientation="horizontal"
    android:weightSum="1"
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/fill_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:background="#000"
        android:orientation="horizontal">
    </LinearLayout>

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/optionItemBackgroundColor"
    android:orientation="vertical"
    android:weightSum="100"
    android:baselineAligned="false">

    <LinearLayout
        android:id="@+id/fill_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:background="@color/percent_fill_color"
        android:orientation="horizontal">
    </LinearLayout>

</LinearLayout>

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

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