Android纠结的布局面试题求解 AndroidrelativeLayout 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:background="#ff0000" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_alignBaseline="@id/button1" android:layout_marginLeft="16dp" android:layout_toRightOf="@+id/button1" android:text="2" android:background="#00ff00" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_toRightOf="@+id/button2" android:layout_alignBottom="@id/button1" android:text="3" android:background="#0000ff" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="22dp" android:text="4" android:background="#33ff77" /></RelativeLayout> <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="100dp" android:layout_toRightOf="@+id/textView1" android:text="TextView" android:textSize="25sp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="18dp" android:layout_toRightOf="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="1dp" android:layout_marginTop="191dp" android:text="TextView" android:textSize="25sp" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView1" android:layout_marginTop="50dp" android:text="TextView" /></RelativeLayout> 有没有不用android:layout_alignBaseline="@+id/textView1"实现呢,因为题目中的1,2,3不一定是按钮或者有文字的控件,可能是图片imageview <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="200dp" > <Button android:id="@+id/btn1" android:layout_width="100dp" android:layout_height="50dp" android:layout_centerVertical="true" android:text="1" /> <Button android:id="@+id/btn2" android:layout_width="80dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_toRightOf="@id/btn1" android:text="2" /> <Button android:id="@+id/btn3" android:layout_width="100dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_toRightOf="@id/btn2" android:text="3" /> <Button android:id="@+id/btn4" android:layout_width="100dp" android:layout_height="50dp" android:layout_alignLeft="@id/btn2" android:layout_below="@id/btn2" android:layout_centerVertical="true" android:layout_marginTop="30dp" android:text="4" /> </RelativeLayout></RelativeLayout> 好多人来回答,看起来挺有意思的,不知道大家是否有考虑过嵌套布局<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/button2" android:layout_toRightOf="@+id/button1" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button3" android:layout_below="@+id/button3" android:layout_marginTop="36dp" android:text="Button" /> </RelativeLayout> </RelativeLayout></LinearLayout> 你发的那个图其实完全可能是这样的情况1,2,3都是水平的,只是1,3设置各自的padding,2没有padding,所以看上去有错落 那就是我在11楼说的情况啊,你现在这张图就是把2的paddingtop和paddingbottom设置得很大,这其实已经不是位置的问题了 那就是我在11楼说的情况啊,你现在这张图就是把2的paddingtop和paddingbottom设置得很大,这其实已经不是位置的问题了如果不是padding,而是所有都是图片,实际上2就是那么大怎么中心对齐呢? 熟悉下面的用法,无论怎么放都可以实现了android:layout_above//将控件的底部置于给定Id的控件之上android:layout_below//将控件的顶部置于给定ID的控件之下android:layout_toLeftOf将该控件的右边缘和给定ID的左边缘对齐android:layout_toRightOf将该控件的左边缘和给定ID的控件的右边缘对齐android:layout_alignBaseline控件的baseline与给定ID控件的baseline对齐android:layout_alignBottom控件的底部边缘与给定ID控件的底部边缘对齐android:layout_alignLeft控件的左边缘与给定ID控件的左边缘对齐android:layout_alignRight控件的右边缘与给定ID控件的右边缘对齐android:layout_alignTop控件的顶部边缘与给定ID控件的顶部对齐android:layout_alignParentBottom控件的底部和父控件的底部对齐android:layout_alignParentLeft控件的左边与父控件的左边对齐android:layout_alignParentRight控件的右边与父控件的右边对齐android:layout_alignParentTop控件的顶部与父控件的顶部对齐android:layout_centerHorizontal置于水平方向的中央android:layout_centerParent如果值为真,置于父控件水平和垂直方向的中央android:layout_centerVertical 如果值为真,该控件将被置于垂直方向的中央 你怎么就是纠结这个问题呢。我也无语了。。<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="5" > <TextView android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="100dp" android:layout_weight="1" android:gravity="center" android:text="TextView" /> <TextView android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="TextView" /> </TableRow></TableLayout>把textview换成imageview,button,这样总合你意了吧。 脸部解锁显示问题………… android程序运行时遇到的一个问题,急。。。。 android中如何向数据库中插入彩信 viewport 没有作用 为什么使用ActivityManager就会报异常 Android入门 android怎么把参数传到asp页面? sqlite数据库的问题 android,xmpp注册的时候怎么同时创建VCard java.lang.NoSuchMethodError: android.support.v7.widget.RecyclerView.findViewHold scrollview tablelayout 使用问题 怎么做Android原生应用开发
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:background="#ff0000" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignBaseline="@id/button1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/button1"
android:text="2"
android:background="#00ff00" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/button2"
android:layout_alignBottom="@id/button1"
android:text="3"
android:background="#0000ff" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="22dp"
android:text="4"
android:background="#33ff77" /></RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="100dp"
android:layout_toRightOf="@+id/textView1"
android:text="TextView"
android:textSize="25sp" /> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/textView1"
android:text="TextView" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="1dp"
android:layout_marginTop="191dp"
android:text="TextView"
android:textSize="25sp" /> <TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView1"
android:layout_marginTop="50dp"
android:text="TextView" /></RelativeLayout>
实现呢,因为题目中的1,2,3不一定是按钮或者有文字的控件,可能是图片imageview
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp" > <Button
android:id="@+id/btn1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:text="1" /> <Button
android:id="@+id/btn2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btn1"
android:text="2" /> <Button
android:id="@+id/btn3"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btn2"
android:text="3" /> <Button
android:id="@+id/btn4"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignLeft="@id/btn2"
android:layout_below="@id/btn2"
android:layout_centerVertical="true"
android:layout_marginTop="30dp"
android:text="4" />
</RelativeLayout></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" /> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button2"
android:layout_toRightOf="@+id/button1" > <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="36dp"
android:text="Button" /> </RelativeLayout> </RelativeLayout></LinearLayout>
1,2,3都是水平的,只是1,3设置各自的padding,2没有padding,所以看上去有错落
android:layout_above//将控件的底部置于给定Id的控件之上
android:layout_below//将控件的顶部置于给定ID的控件之下
android:layout_toLeftOf将该控件的右边缘和给定ID的左边缘对齐
android:layout_toRightOf将该控件的左边缘和给定ID的控件的右边缘对齐
android:layout_alignBaseline控件的baseline与给定ID控件的baseline对齐
android:layout_alignBottom控件的底部边缘与给定ID控件的底部边缘对齐
android:layout_alignLeft控件的左边缘与给定ID控件的左边缘对齐
android:layout_alignRight控件的右边缘与给定ID控件的右边缘对齐
android:layout_alignTop控件的顶部边缘与给定ID控件的顶部对齐
android:layout_alignParentBottom控件的底部和父控件的底部对齐
android:layout_alignParentLeft控件的左边与父控件的左边对齐
android:layout_alignParentRight控件的右边与父控件的右边对齐
android:layout_alignParentTop控件的顶部与父控件的顶部对齐
android:layout_centerHorizontal置于水平方向的中央
android:layout_centerParent如果值为真,置于父控件水平和垂直方向的中央
android:layout_centerVertical 如果值为真,该控件将被置于垂直方向的中央
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" > <TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" > <TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="TextView" /> <TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" /> <TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="TextView" />
</TableRow></TableLayout>
把textview换成imageview,button,这样总合你意了吧。