一步一步学android之布局管理器——RelativeLayout

今天开始学习RelativeLayout(相对布局),相对布局在平时布局的时候用的较多,因为Android适配方面的原因。相对布局可以控制组件摆放的位置(放在任一组件的上下左右等位置),下面来看看类的定义(http://developer.android.com/reference/android/widget/RelativeLayout.html):


相对布局有几个非常常用的属性在这里列个表格说下:

RelativeLayout的常用属性
编号 属性名称 对应的变量 意义
1 android:layout_below RelativeLayout.BELOW 放在指定组件的下面
2 android:layout_toLeftOf RelativeLayout.LEFT_OF 放在指定组件的左边
3 android:layout_toRightOf RelativeLayout.RIGHT_OF 放在指定组件的右边
4 android:alignTop RelativeLayout.ALIGN_TOP 以指定组件为参考进行上对齐
5 android:alignButtom RelativeLayout.ALIGN_BUTTOM 以指定组件为参考进行下对齐
6 android:alignLeft RelativeLayout.ALIGN_LEFT 以指定组件为参考进行左对齐
7 android:alignRight RelativeLayout.ALIGN_RIGHT 以指定组件为参考进行右对齐



下面同样写个例子来说明。

效果如下:



我用RelativeLayout来自定义title(比较常见)。

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:background="@drawable/head_bg" >

        <ImageButton
            android:id="@+id/main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/main" />

        <ImageButton
            android:id="@+id/fresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/fresh" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="Kay"
            android:textColor="#FFFFFFFF"
            android:textSize="24sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1" />

</LinearLayout>


上面用到的属性有一个上面没有说到就是android:layout_alignParentLeft( layout_alignParentRight)android:layout_alignParentTop分别表示相对父布局靠左(靠右)和靠上,至于消除系统title前面已经说过了(http://blog.csdn.net/kaypro/article/details/9858807


接下来就是和以前一样用java代码来实现上面的效果:

package com.example.relativelayoutdemo;

import android.os.Bundle;
import android.provider.Contacts.Organizations;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.app.Activity;
import android.graphics.Color;
import android.text.Layout;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 获取密度(用于dp转换成px值)
		float scale = this.getResources().getDisplayMetrics().density;
		// setContentView(R.layout.activity_main);
		// 定义LinearLayout装载RelativeLayout和ImageView
		LinearLayout lLayout = new LinearLayout(this);
		// 设置垂直显示
		lLayout.setOrientation(LinearLayout.VERTICAL);
		// 定义LinearLayout的高和宽
		LinearLayout.LayoutParams lLayoutParams = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		
		// 定义RelativeLayout
		RelativeLayout rLayout = new RelativeLayout(this);
		// 定义RelativeLayout的高和宽
		RelativeLayout.LayoutParams rLayoutParams = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, (int) (45 * scale + 0.5f));
		// 设置RelativeLayout的背景
		rLayout.setBackgroundResource(R.drawable.head_bg);
		
		// 定义两个ImageButton
		ImageButton main = new ImageButton(this);
		ImageButton fresh = new ImageButton(this);
		// 定义两个ImageButton的高和宽
		RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		RelativeLayout.LayoutParams freParams = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		// 设置两个ImageButton的背景
		main.setBackgroundResource(R.drawable.main);
		fresh.setBackgroundResource(R.drawable.fresh);
		// 为两个ImageButton添加规则
		// 在父布局的右边(相当于android:alignParentRight="true")
		mainParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		// 垂直居中显示
		mainParams.addRule(RelativeLayout.CENTER_VERTICAL);
		// 在父布局的左边(android:alignParentLeft="false")
		freParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		freParams.addRule(RelativeLayout.CENTER_VERTICAL);
		// layout_marginLeft="10dp"(这里dp转换成像素)
		freParams.leftMargin = (int) (10 * scale + 0.5f);
		
		TextView tv = new TextView(this);
		tv.setGravity(Gravity.CENTER);
		tv.setTextColor(Color.WHITE);
		tv.setTextSize(24);
		tv.setText("Kay");
		
		// 定义ImageView和设置背景图
		ImageView img = new ImageView(this);
		img.setBackgroundResource(R.drawable.img_1);
		// RelativeLayout添加main fresh tv这三个view
		rLayout.addView(fresh, freParams);
		rLayout.addView(tv, lLayoutParams);
		rLayout.addView(main, mainParams);

		// LinearLayout添加RelativeLayout和ImageView
		lLayout.addView(rLayout, rLayoutParams);
		lLayout.addView(img, lLayoutParams);
		
		// 显示
		super.setContentView(lLayout, lLayoutParams);

	}

}

效果和上面xml实现的一样,好了,今天就说到这里了。


原文地址:https://www.cnblogs.com/pangblog/p/3275714.html