如何将ImageView与右侧对齐?

时间:2016-12-12 07:17:46

标签: android cardview

我想在cardview内部右侧设置一个imageview但是对齐不起作用...请建议我

布局

  <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>

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

7 个答案:

答案 0 :(得分:0)

通过执行

来拉伸TextView
android:layout_width="0dp"
android:layout_weight="1"

答案 1 :(得分:0)

您可以使用以下代码。

  <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文件

答案 2 :(得分:0)

只需使用简单的重力进行图像查看。

<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>

答案 3 :(得分:0)

请使用此代码将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>

答案 4 :(得分:0)

请检查以下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>

答案 5 :(得分:0)

使用RelativeLayout代替LinearLayout 并添加

android:layout_alignParentRight="true"

ImageView

多数民众赞成!!

答案 6 :(得分:0)

在Cardview内部,容器LinearLayout只给出Weightsum为1,并且Imageview的权重为.25,textview的权重为.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>
相关问题