我如何在Android中正确地对齐东西?
我正在尝试布局一个简单的CardView,上面有文本,下面有几个“按钮”。但我甚至无法使布局正确对齐,我在过去(我必须承认,一段时间前)使用了上面的布局和下面的布局,但它们似乎不再可用。所以我尝试了其他一些方法,基于我从文档中学到的,但我认为我学到的不多。。。显然 为了更好地解释我的代码试图做什么,这里有一张图片: 我看到的不是上面图片中的内容,而是两个布局重叠,按钮出现在两个布局的顶部我如何在Android中正确地对齐东西?,android,xml,Android,Xml,我正在尝试布局一个简单的CardView,上面有文本,下面有几个“按钮”。但我甚至无法使布局正确对齐,我在过去(我必须承认,一段时间前)使用了上面的布局和下面的布局,但它们似乎不再可用。所以我尝试了其他一些方法,基于我从文档中学到的,但我认为我学到的不多。。。显然 为了更好地解释我的代码试图做什么,这里有一张图片: 我看到的不是上面图片中的内容,而是两个布局重叠,按钮出现在两个布局的顶部 <?xml version="1.0" encoding="utf-8"?> <andr
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="72dp"
card_view:cardCornerRadius="0.7dp"
card_view:cardElevation="1.0dp"
android:background="@color/cardview_light_background"
android:layout_marginBottom="7dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:background="@color/linearLayoutBackgroundColor">
<TextView
android:id="@+id/cardViewTextItem"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cardText"
android:padding="7dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:background="@color/secondLinearLayoutBackgroundColor">
<Button android:text="DETAILS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/cardview_light_background" />
</RelativeLayout>
</android.support.v7.widget.CardView>
将所有内容包装在
线性布局中
并删除嵌套的相对布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="72dp"
card_view:cardCornerRadius="0.7dp"
card_view:cardElevation="1.0dp"
android:background="@color/cardview_light_background"
android:layout_marginBottom="7dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true">
<TextView
android:id="@+id/cardViewTextItem"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cardText"
android:padding="7dp"/>
<Button android:text="DETAILS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/cardview_light_background" />
</android.support.v7.widget.CardView>
</LinearLayout>
试试这个,很简单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dip"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
style="@style/Widget.CardContent.TitleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textWelCome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="cardText"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
style="@style/Widget.CardContent.TitleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/textWelCome2"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_marginLeft="175dip"
android:layout_marginTop="2dp"
android:text="DETAILS"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
上面的布局和下面的布局仅在RelativeLayout中可用。您的xml应该如下所示:
<android.support.v7.widget.CardView
/*some attributes*/ >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/button"/>
</RelativeLayout>
</android.support.v7.widget.CardView>