在布局android中水平对齐2个元素

时间:2013-11-06 06:03:43

标签: android xml layout

我希望在linearlayout中水平放置评级栏和textview。 事情就像评级栏小于textview,我没有得到正确的对齐。 我该怎么办???

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

<TextView
    android:id="@+id/author_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@id/author_name"
    >

    <RatingBar
        android:id="@+id/rtbProductRating"
        style="@style/SmallRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:numStars="5"

        android:layout_marginBottom="@dimen/padding_text"
        android:layout_marginLeft="@dimen/padding_text"
        android:rating="0" />

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/padding_text"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textSize="18sp" >
    </TextView>
</LinearLayout>
<TextView
        android:id="@+id/texto"
        android:layout_below="@id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

4 个答案:

答案 0 :(得分:0)

试试这......在RatingBar上更改...将layout_width更改为match_parent

<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"

android:layout_below="@id/author_name"
>

<RatingBar
    android:id="@+id/rtbProductRating"
    style="@style/SmallRatingBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isIndicator="true"
    android:numStars="5"
    android:layout_marginBottom="@dimen/padding_text"
    android:layout_marginLeft="@dimen/padding_text"
    android:rating="0" />

<TextView
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/padding_text"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:textSize="18sp" >
</TextView>

答案 1 :(得分:0)

在我看来,谷歌建议,在相对布局中使用linearlayout不是一个好主意,你可以使用这样的相对布局来实现它:

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

<TextView
    android:id="@+id/author_name"
    android:text="d"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/texto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/author_name" />

<RatingBar
    android:id="@+id/rtbProductRating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/texto"
    android:isIndicator="true"
    android:numStars="5"
    android:rating="0" />

<TextView
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/texto"
    android:layout_toRightOf="@id/rtbProductRating"
    android:gravity="center_horizontal|center_vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:text="asdf"
    android:textSize="18sp" />

</RelativeLayout>

不幸的是,我不明白你的意思是说RatingBar小于textview!当他们居中时会怎么样?

答案 2 :(得分:0)

请参阅下面的代码,我对它进行了一些修改,以便我可以在我身边进行测试。它的工作正常。您可以使用此代码。请相应修改。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/author_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/author_name"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <RatingBar
            android:id="@+id/rtbProductRating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:isIndicator="true"
            android:numStars="5"
            android:rating="0" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="8dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:text="test"
            android:textColor="@color/white"
            android:textSize="18sp" >
        </TextView>
    </LinearLayout>

    <TextView
        android:id="@+id/texto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll1" />

</RelativeLayout>

答案 3 :(得分:0)

使用线性布局 您可以使用 机器人:比重= “中心”

以下布局可以达到您想要的效果:

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

<TextView
    android:id="@+id/author_name"
    android:text="d"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@id/author_name"
    >

    <RatingBar
        android:id="@+id/rtbProductRating"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:isIndicator="true"
        android:numStars="5"
        android:rating="0" />

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="sdf"
        android:textSize="18sp" >

]
    </TextView>
</LinearLayout>
<TextView
        android:id="@+id/texto"
        android:layout_below="@id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>
相关问题