Android 如何对齐文本视图右侧的小方形按钮?

Android 如何对齐文本视图右侧的小方形按钮?,android,android-layout,Android,Android Layout,这可能是一个愚蠢的问题,但我找不到解决问题的任何方法,让我解释我的问题,我正在尝试将小方形按钮与文本右侧对齐,如下图所示。我尝试了许多方法,但没有任何方法帮助我发布我迄今为止尝试过的内容: <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/content_spare_request"

这可能是一个愚蠢的问题,但我找不到解决问题的任何方法,让我解释我的问题,我正在尝试将小方形按钮与文本右侧对齐,如下图所示。我尝试了许多方法,但没有任何方法帮助我发布我迄今为止尝试过的内容:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/content_spare_request"
        android:background="#fff"
        android:layout_marginBottom="20dp"
        android:layout_margin="10dp"
        android:layout_below="@+id/root1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:id="@+id/spare_text"
            android:layout_marginStart="2sp"
            android:paddingLeft="10dp"
            android:layout_marginTop="07dp"
            android:text="@string/Spare"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/SForange"
            android:textSize="16sp" />
        <Button
            android:id="@+id/btn_spare"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:background="@color/SForange"
            android:layout_alignParentRight="true"
            />

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:id="@+id/view2"
            android:layout_below="@+id/spare_text"
            android:background="@color/SForange"
            android:gravity="center" />
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/spare_recyclerview"
            android:layout_below="@+id/view2"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="75dp"
            android:gravity="center_horizontal"
            android:layout_below="@+id/view2"
            android:visibility="visible"
            android:text="No Data Available"
            />

    </RelativeLayout>

您可以执行以下操作

  • 将relativelayout的宽度设为匹配父项
  • 将btn_备用设置为右对齐,并将textview设置为btn_备用的开始
  • 使用。它使您能够定义视图的确切位置。例如,您希望在imageview下有一个按钮,按钮之间有特定的距离,ConstraintLayout将为您提供如下内容


    不要使用
    match\u parent
    作为按钮高度。将您的
    按钮
    高度从
    match_parent
    更改为特定高度,并向其添加
    android_background

        <Button
        android:id="@+id/btn_spare"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:background="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        />
    
    
    
    输出:

    尝试过这个

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_spare_request"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/root1"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:layout_marginBottom="20dp"
        android:background="#fff">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/spare_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="2sp"
                android:layout_marginTop="07dp"
                android:paddingLeft="10dp"
                android:text="@string/tip_brush_size"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/facebook"
                android:textSize="16sp" />
    
            <Button
                android:id="@+id/btn_spare"
                android:layout_width="20dp"
                android:layout_height="match_parent"
                android:background="@color/facebook"
                android:layout_alignParentRight="true"
                android:minHeight="0dp"
                android:minWidth="0dp" />
        </RelativeLayout>
    
    
        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_below="@+id/spare_text"
            android:background="@color/facebook"
            android:gravity="center" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/spare_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/view2" />
    
        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:gravity="center_horizontal"
            android:paddingBottom="75dp"
            android:text="No Data Available"
            android:visibility="visible" />
    
    </LinearLayout>
    
    
    
    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_spare_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/root1"
        android:layout_margin="10dp"
        android:layout_marginBottom="20dp"
        android:background="#fff">
    
        <LinearLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/spare_text"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:paddingLeft="10dp"
                android:text="@string/Spare"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/SForange"
                android:textSize="16sp" />
    
            <Button
                android:id="@+id/btn_spare"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:background="@color/SForange"
                android:minHeight="0dp"
                android:minWidth="0dp" />
    
        </LinearLayout>
    
    
        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_below="@+id/header"
            android:background="@color/SForange"
            android:gravity="center" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/spare_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/view2" />
    
        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/view2"
            android:gravity="center_horizontal"
            android:paddingBottom="75dp"
            android:text="No Data Available"
            android:visibility="visible" />
    
    </RelativeLayout>
    
    
    
    使用
    drawableRight
    您能否详细说明@hrskrs然后如何使其成为可点击的按钮您希望加号按钮位于右侧?是的,如上图所示@ajaysrest更改颜色名称。您需要将文本视图和按钮放在另一个相对布局中。这样做会有用的