如何使cardview中的imageview对齐左侧

时间:2016-06-09 10:59:39

标签: android user-interface

这是我的代码:

<android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentPaddingLeft="@dimen/activity_horizontal_margin"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp">
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="horizontal">
        <ImageView android:layout_width="match_parent" android:src="@drawable/ic_open_in_new_black_48dp"
                   android:layout_height="match_parent"/>
        <TextView android:text="in card view" android:layout_gravity="center_vertical"
                  android:layout_width="match_parent" android:layout_height="wrap_content"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

我希望第一行使用第一个ImageView和第二个TextView,但我得到:

enter image description here

如何解决?

2 个答案:

答案 0 :(得分:1)

您正在使用match_parent中的android:layout_widthandroid:layout_height进行imageview。 将其更改为wrap_content。

答案 1 :(得分:0)

使用RelativeLayout取代LinearLayout,将用户“alignParentLeft”用于ImageView

<android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentPaddingLeft="@dimen/activity_horizontal_margin"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp">
    <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
        <ImageView 
          android:layout_width="match_parent"      
          android:src="@drawable/ic_open_in_new_black_48dp"
          android:layout_height="match_parent"
          android:alignParentLeft="true"
        />
        <TextView 
          android:text="in card view" 
          android:layout_gravity="center_vertical"
          android:layout_width="match_parent"  
          android:layout_height="wrap_content"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>