Android 在cardview中对齐项目
我想让我的卡片看起来像下面这样 我保持这样的布局Android 在cardview中对齐项目,android,android-layout,android-linearlayout,android-cardview,Android,Android Layout,Android Linearlayout,Android Cardview,我想让我的卡片看起来像下面这样 我保持这样的布局 <android.support.v7.widget.CardView android:layout_gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" card_view:cardCornerRadius="2dp" >
<android.support.v7.widget.CardView
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Order# GAMH2103"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:gravity="start"
android:textSize="15dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirmed"
android:drawableRight="@drawable/check"
android:textColor="#00FF00"
android:gravity="end"
android:textSize="15dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="5dp"
/>
</LinearLayout>
不知何故,我无法使“确认”文本视图可见。不过我能看到订单
我玩过gravity
和layou-gravity
,但不知何故还是没能通过
请帮忙
谢谢,拉克希曼。一个简单的相对布局就可以了
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Order# GAMH2103"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:gravity="start"
android:textSize="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirmed"
android:drawableRight="@drawable/check"
android:textColor="#00FF00"
android:layout_gravity="end"
android:textSize="15dp"/>
</RelativeLayout>
请注意
gravity
和layout\u gravity
之间的区别,如果在TextView
中使用gravity
,则它与不可见框中的文本位置有关,该框是TextView
的边界,例如,另一方面,布局与视图在父视图中的位置有关。使用以下布局。这将适用于您
为了更好地对齐视图,最好使用相对布局而不是线性布局
<android.support.v7.widget.CardView
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Order# GAMH2103"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:gravity="start"
android:textSize="15dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirmed"
android:textColor="#00FF00"
android:gravity="end"
android:textSize="15dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
因为您设置了“订单”宽度,尽管“文本视图”是android:layout\u width=“match\u parent”,所以“确认”文本视图不可见。更改为android:layout\u width=“wrap\u content”。如果希望“已确认”文本视图始终可见,可以添加android:layout\u weight=“1”
。