[英]Difficult in aligning textview and ratingbar
我對XML不太了解,我需要幫助來對齊兩個元素,即textview和ratingbar。 我將等級欄(五顆星)與textview(“用戶”)的水平對齊。 這個怎么做?
這是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>
<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>
嘗試這個:
<?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>
使用TextView上的android:layout_alignBottom =“ @ + id / textRatingBar”和RatingBar上的android:paddingBottom =“ 2dp”,我獲得了出色的對齊方式((您可能必須根據字體大小調整值)
這是布局文件的摘錄:
<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" />
這可能會遲到,但是對於那些將來可能會有所幫助的人。 這是我可以實現的方法。
實現它需要應用的主要屬性是
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”
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.