【问题标题】:LinearLayout Fill Percent Of BackgroundLinearLayout 填充背景百分比
【发布时间】:2017-04-16 22:05:52
【问题描述】:

我正在尝试填充(水平)项目背景的一定百分比。

我已经尝试过很多人推荐的方法:

<?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 背景布局落后于其余布局?这是我的完整 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>

【问题讨论】:

  • 请重新定义您的问题,因为不清楚您要做什么/问什么。还链接到您说尝试过的其他 SO 问题。这将有助于了解上下文。
  • 你想给你的Linearlayout背景设置颜色???
  • 我想将我的线性布局背景的百分比填充为一种颜色。百分比应该能够以编程方式更改。
  • 对于 EX,你有 2 个线性布局 1 占 80%,2 占 20% ??

标签: java android background android-linearlayout percentage


【解决方案1】:

如果你想达到这样的效果,试试这个代码

<?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>

如果你想获得这个视图试试这个

<?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>

【讨论】:

  • 那种。当我添加了内部线性布局时,它最终会移动我的布局的其余部分已经在线性布局中。
  • 你能发一张你得到的截图吗
  • 你要获取第一个视图还是第二个视图的那个
  • 第二。顺便说一句,这是一个列表项。
  • 你能不能更具体一点,如果你上传一个你正在得到的和你想要实现的屏幕截图会很有帮助
【解决方案2】:
<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>

【讨论】:

  • 对于 EX,你有 2 个线性布局 1 占 80%,2 占 20% ??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2011-02-22
相关资源
最近更新 更多