Android布局小结

1.FrameLayout

帧布局,效果为多个图层依次叠加,比如说画一幅画,在FrameLayout中先添加背景imageView,再添加其他的事物imageView即可。

如何实现三个按钮在水平方向上左对齐、居中对其和右对齐?

在FrameLayout中可对每个View设置Layout_gravity属性实现

code:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="btn1" />
    
     <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="btn2" />
     
      <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="btn3" />
    
</FrameLayout>
效果图:


2.LinearLayout布局

在LinearLayout中添加分界线:

通过设置android:ShowDividers属性(none/beginning/end/middle)和android:drivider属性(Drawable ID)

如何在LinearLayout中实现三个按钮水平方向分别居于左中右位置?

设置Button的layout_weight属性即可

3.RelativeLayout布局

实现5个按钮梅花状排序。

效果图:

code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_toRightOf="@+id/btn1"
        android:text="btn2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn2"
        android:layout_toRightOf="@+id/btn2"
        android:text="btn3" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_toLeftOf="@+id/btn2"
        android:text="btn4" />
    
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_toRightOf="@+id/btn2"
        android:text="btn5" />

</RelativeLayout>

如何动态改变控件在布局中的位置?

通过LayoutParams实现。

code:

btn = (Button) findViewById(R.id.btn1);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT,
						ViewGroup.LayoutParams.WRAP_CONTENT);
				if(i == 1){
					i = 0;
					lp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
				}
				else{
					i = 1;
					lp.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
				}
				btn.setLayoutParams(lp);
			}
		});
4.设置背景渐变色

GradientDrawable,通过向函数传入渐变方向及颜色数组设置背景渐变情况

code:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		int[] col = {Color.BLUE,Color.BLACK};
		GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, col);
		getWindow().setBackgroundDrawable(gd);
	}
效果图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值