Android对话框削减评级栏

时间:2015-10-08 11:09:35

标签: android xml

我有一个自定义对话框,可用作我应用中商品的评级窗口。该对话框有一个标题栏,一个评级栏,一个文本编辑器和两个按钮。标题栏是动态的,并根据被评级的项目进行设置。当项目具有“长”名称时,例如至少15个字符,评级栏将正确显示。

The custom dialog box with a long title

当标题标题较短时,评级栏会被删除。

custom dialog box with an incomplete rating bar

我尝试使用固定大小的对话框,但它看起来很糟糕,而且事情变得错位。

以下是对话框的XML:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp"
        android:textColor="@color/dim_foreground_material_light"
        android:text="Rate Spongy's Cafe"
        android:id="@+id/rateHeader"
        android:gravity="center"
        android:layout_centerHorizontal="true" />
    <View
        android:layout_width="match_parent"
        android:background="@color/myPrimaryColor"
        android:layout_height="1dp"
        android:id="@+id/separator"
        android:layout_below="@+id/rateHeader"
        android:layout_marginTop="5dp" />
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingsBar"
        android:clickable="false"
        android:rating="5"
        style="@style/MyRatingBar"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_below="@+id/separator"
        android:layout_alignRight="@+id/rateHeader"
        android:layout_alignEnd="@+id/rateHeader" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5.0"
        android:id="@+id/rateTV"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="20sp"
        android:layout_below="@+id/ratingsBar"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/errorTV"
        android:text="Something Went Wrong. Please Try Again"
        android:layout_below="@+id/rateTV"
        android:textColor="@color/white"
        android:visibility="gone"
        android:layout_centerHorizontal="true"
        android:background="@color/errorColor"/>

    <EditText
        android:layout_below="@+id/errorTV"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Write Your Review(200 Characters)"
        android:lines="5"
        android:id="@+id/reviewED"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reviewED"
        android:layout_gravity="center"
        android:weightSum="2">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Cancel"
            android:background="@drawable/cart_button_selector"
            android:id="@+id/cancelRateBtn"
            android:textColor="@color/myPrimaryColor"
            android:layout_weight="1"
            android:layout_alignParentRight="true"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Submit"
            android:textColor="@color/myPrimaryColor"
            android:background="@drawable/cart_button_selector"
            android:layout_weight="1"
            android:id="@+id/submitRateBtn"
            />

    </LinearLayout>


</RelativeLayout>

java代码:

dialog = new Dialog(RateRestaurantActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.rate_dialog);
TextView titleTV = (TextView)dialog.findViewById(R.id.rateHeader);
final TextView rateTV = (TextView)dialog.findViewById(R.id.rateTV);
Button submitBtn = (Button)dialog.findViewById(R.id.submitRateBtn);
Button cancelBtn = (Button)dialog.findViewById(R.id.cancelRateBtn);
RatingBar ratingBar = (RatingBar)dialog.findViewById(R.id.ratingsBar);
final EditText reviewED = (EditText)dialog.findViewById(R.id.reviewED);
titleTV.setText("Rate "+title);
dialog.show();

请帮忙?感谢

1 个答案:

答案 0 :(得分:3)

name的TextView内容android:layout_width="wrap_content"保持为android:id="@+id/rateHeader"fill_parent

和你的评级栏一样,你可以做到!

所以最终的代码应该是

match_parent