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" > </frameLayout>效果图:
2.LinearLayout布局
在LinearLayout中添加分界线:
通过设置android:ShowDividers属性(none/beginning/end/middle)和android:drivider属性(Drawable ID)
如何在LinearLayout中实现三个按钮水平方向分别居于左中右位置?
设置Button的layout_weight属性即可
3.RelativeLayout布局
实现5个按钮梅花状排序。
效果图:
code:
通过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); }效果图: