1

晚上好。

我现在已经摆弄了一个多小时,但我没有让它工作。我想创建一个应该如下所示的布局:

在此处输入图像描述

我想让一个 TextView 向左对齐,一个 ProgressBar 居中,另一个 TextView 向右对齐。我不想使用静态宽度,因为布局应该用于不同的设备。

不幸的是,我的布局目前看起来像这样: 在此处输入图像描述

我没有通过谷歌找到任何解决方案,所以非常感谢任何帮助。这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25sp"
            android:layout_marginRight="5sp"
            android:gravity="center"
            android:text="0 %" />

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.36" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="25sp"
            android:gravity="right"
            android:text="100 %" />
    </LinearLayout>

</LinearLayout>
4

3 回答 3

2

你可以试试RelativeLayout这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/yourTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20pt"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/yourTitle">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25sp"
            android:layout_marginRight="5sp"
            android:gravity="center"
            android:text="0 %" />

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.36" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="25sp"
            android:gravity="right"
            android:text="100 %" />
        </LinearLayout>

</RelativeLayout>

这会产生这样的东西:

来自模拟器的布局图像

于 2013-10-18T19:58:12.850 回答
1

我刚刚添加weight到文本视图并设置了它们的width0dp

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25sp"
            android:layout_marginRight="5sp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="0 %" />

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="25sp"
            android:layout_weight="1.2"
            android:gravity="right"
            android:text="100 %" />
    </LinearLayout>

</LinearLayout>
于 2013-10-18T20:02:25.150 回答
1

我试过你的代码,它对我来说可以正常工作。但是您可以尝试android:layout_width在 ProgressBar 中更改为 0dip。

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
于 2013-10-18T20:04:16.773 回答