简体   繁体   中英

How to Align an ImageView to the right side?

I want to set an imageview to the right side inside the cardview but aligning is not working... please suggest me

Layout

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

Like this way i am trying but it is not working and i don't know why this is not working

Make your TextView stretched by doing

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

You can use following code.

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

You can copy and paste it your xml file

Just use simple gravity for your image view.

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

Please use this code to set Imageview to right side inside CardView, just using the Relative layout.

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

Please check the following 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>

Use RelativeLayout instead of LinearLayout and add

android:layout_alignParentRight="true"

to the ImageView

thats all !!

Inside the Cardview, the container LinearLayout just give Weightsum of 1 and give both the Imageviews a weight of .25 and textview a weight of .5 as shown below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM