难以对齐textview和ratingbar

问题描述 投票:2回答:4

我不是那么专业的XML,我需要帮助来对齐两个元素,textview和ratingbar。我想将ratingbar(五颗星)与textview(“user”)的同一级别对齐。这该怎么做?

这是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@drawable/evento_pressione" >


    <LinearLayout
        android:id="@+id/viewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:background="@drawable/green_button_pressed" >

        <TextView
            android:id="@+id/nameMerchant"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="Users list"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#f6911e"
            android:textStyle="bold" tools:ignore="HardcodedText"/>
    </LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/viewName" >

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/userM"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_alignTop="@+id/userM"
        android:src="@drawable/arrow" />

    <TextView
        android:id="@+id/userM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="20dp"
        android:layout_marginLeft="21dp"
        android:text="Comune"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4a4c4d"
        tools:ignore="HardcodedText" />

    <RatingBar
        android:id="@+id/rateM"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/arrow"
        android:layout_marginRight="30dp"
        android:layout_margin="20dp"
        android:indeterminate="false"
        android:isIndicator="true"
        android:numStars="@integer/five"
        android:stepSize="1.0" />

    </RelativeLayout>
</RelativeLayout>
android xml textview alignment ratingbar
4个回答
0
投票
<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"
android:gravity="left" >

<LinearLayout
    android:id="@+id/viewName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="40dp" >

    <TextView
        android:id="@+id/nameMerchant"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:text="Users list"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#f6911e"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/viewName" >

    <TextView
        android:id="@+id/userM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="20dp"
        android:layout_marginLeft="21dp"
        android:text="Comune"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4a4c4d"
        tools:ignore="HardcodedText" />

    <RatingBar
        android:id="@+id/rateM"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="20dp"
        android:layout_marginRight="30dp"
        android:indeterminate="false"
        android:isIndicator="true"
        android:numStars="5"
        android:stepSize="1.0" />

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/userM"
        android:layout_alignParentRight="true"
        android:src="@drawable/icon" />
</RelativeLayout>


0
投票

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@drawable/evento_pressione" >


    <LinearLayout
        android:id="@+id/viewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:background="@drawable/green_button_pressed" >

        <TextView
            android:id="@+id/nameMerchant"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="Users list"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#f6911e"
            android:textStyle="bold" tools:ignore="HardcodedText"/>
    </LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/viewName" >

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/userM"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_alignTop="@+id/userM"
        android:src="@drawable/arrow" />

    <TextView
        android:id="@+id/userM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="20dp"
        android:layout_marginLeft="21dp"
        android:layout_centerVertical="true"
        android:text="Comune"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4a4c4d"
        tools:ignore="HardcodedText" />

    <RatingBar
        android:id="@+id/rateM"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/arrow"
        android:layout_marginRight="30dp"
        android:layout_margin="20dp"
        android:layout_centerVertical="true"
        android:indeterminate="false"
        android:isIndicator="true"
        android:numStars="@integer/five"
        android:stepSize="1.0" />

    </RelativeLayout>
</RelativeLayout>

0
投票

我在TextView上使用android:layout_alignBottom =“@ + id / textRatingBar”和在RatingBar上使用android:paddingBottom =“2dp”进行了很好的对齐,(你可能需要根据你的字体大小调整值)enter image description here

这里是布局文件的摘录:

            <TextView
            android:id="@+id/textRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textDealTime"
            android:layout_marginLeft="5dp"
            android:text="Rating"
            android:textColor="@color/dark_gray"
            android:textSize="14sp" />
        <RatingBar
            android:id="@+id/ratingBar"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="2dp"
            android:numStars = "5"
            android:stepSize="0.1"
            android:isIndicator="true"
            android:progressBackgroundTint="@color/main_teal"
            android:progressTint="@color/main_teal"
            android:secondaryProgressTint="@color/main_teal"
            android:layout_alignBottom="@+id/textRatingBar"
            android:layout_toRightOf="@+id/textRatingBar"
            android:layout_toEndOf="@+id/textRatingBar" />

0
投票

这可能会迟到,但对于那些可能在将来有所帮助的人。这是我如何实现这一目标的。

要实现它的主要属性是

        android:layout_alignBottom="@+id/your_rating_bar_id"
        android:layout_alignTop="@+id/your_rating_bar_id"
        android:gravity="center_vertical"

将它放在TextView元素上,确保它的高度没有约束,它将垂直对齐它的中心与评级栏元素的中心

ID = “your_rating_bar_id”

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.