Android 如何将ImageView与右侧对齐?

Android 如何将ImageView与右侧对齐?,android,cardview,Android,Cardview,我想将imageview设置到cardview内的右侧,但对齐不起作用。。。请推荐我 布局 <FrameLayout android:id="@+id/framelayout" android:layout_height="match_parent" android:layout_width="match_parent"/> <LinearLayout android:layout_width="match_parent"

我想将imageview设置到cardview内的右侧,但对齐不起作用。。。请推荐我

布局

  <FrameLayout
    android:id="@+id/framelayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lin1"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp"
            android:id="@+id/casardviewvisit">
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#482048"
                    android:orientation="horizontal">
                <ImageView
                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"
                    android:src="@drawable/omg"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20dp"
                    android:textColor="#fff"
                    android:textStyle="bold"
                    android:layout_marginLeft="20dp"
                    android:layout_gravity="center"
                    android:text="Add Order"/>

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/sent"
                        android:layout_gravity="right"
                        android:id="@+id/imageButtsdon" />
                </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
  </framelayout>


像这样,我正在尝试,但它不起作用,我不知道为什么它不起作用

android:layout_width="0dp"
android:layout_weight="1"

您可以使用以下代码

  <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:layout_marginLeft="05dp"
    android:layout_marginTop="15dp"
    android:id="@+id/casardviewvisit">

    <LinearLayout
        android:weightSum="1"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#482048"
        android:orientation="horizontal">
        <ImageView
            android:layout_weight=".2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:src="@drawable/test"/>
        <TextView
            android:layout_weight=".5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            android:layout_gravity="center"
            android:text="Add Order"/>

        <ImageView
            android:layout_weight=".2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:src="@drawable/test"
            android:layout_gravity="right"
            android:id="@+id/imageButtsdon" />
    </LinearLayout>
</android.support.v7.widget.CardView>


您可以将其复制并粘贴到您的xml文件中

只需在图像视图中使用简单重力即可

<FrameLayout
    android:id="@+id/framelayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lin1"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="05dp"
                android:layout_marginTop="15dp"
                android:id="@+id/casardviewvisit">
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#482048"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="20dp"
                        android:textColor="#fff"
                        android:textStyle="bold"
                        android:layout_marginLeft="20dp"
                        android:layout_gravity="center"
                        android:text="Add Order"/>

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/sent"
                        android:layout_gravity="right"
                        android:id="@+id/imageButtsdon" />
                </LinearLayout>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_gravity="end"
                    android:layout_height="match_parent"
                    android:layout_marginRight="16dp"
                    android:src="@drawable/omg"/>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

请使用此代码将Imageview设置为CardView内的右侧,只需使用相对布局即可

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/casardviewvisit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#482048">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:text="Add Order"
                    android:textColor="#fff"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <ImageButton
                    android:id="@+id/imageButtsdon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:src="@drawable/beautiful" />
            </RelativeLayout>
        </android.support.v7.widget.CardView>

请检查以下xml

<FrameLayout
        android:id="@+id/framelayout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lin1"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="05dp"
                android:layout_marginTop="15dp"
                android:id="@+id/casardviewvisit">
                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#482048"
                        android:orientation="horizontal">

                   <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="#482048"
                        android:orientation="horizontal">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="20dp"
                        android:textColor="#fff"
                        android:textStyle="bold"
                        android:layout_marginLeft="20dp"
                        android:layout_gravity="center"
                        android:text="Add Order"/>

                    <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/sent"
                            android:layout_gravity="right"
                            android:id="@+id/imageButtsdon" />
                    </LinearLayout>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/omg"/>
                    </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
      </framelayout>

使用
RelativeLayout
而不是
线性布局

转到
ImageView


就这些

在Cardview内部,容器线性布局只需给出权重和1,并将ImageView和textview的权重分别设置为.25和.5,如下所示

    <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/casardviewvisit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#482048"
                android:orientation="horizontal"
                android:weightSum="1" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".25"
                    android:src="@drawable/omg" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_weight=".5"
                    android:text="Add Order"
                    android:textColor="#fff"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <ImageButton
                    android:id="@+id/imageButtsdon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight=".2"
                    android:src="@drawable/sent" />
            </LinearLayout>
    </android.support.v7.widget.CardView>


您可以使文本视图布局宽度=0dp,布局重量=1。。。告诉我,如果它对yahyup有效的话,它的效果会非常好。
    <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/casardviewvisit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="05dp"
            android:layout_marginTop="15dp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#482048"
                android:orientation="horizontal"
                android:weightSum="1" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight=".25"
                    android:src="@drawable/omg" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_weight=".5"
                    android:text="Add Order"
                    android:textColor="#fff"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <ImageButton
                    android:id="@+id/imageButtsdon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight=".2"
                    android:src="@drawable/sent" />
            </LinearLayout>
    </android.support.v7.widget.CardView>