Android应用布局

一、抽象布局merge

merge布局可以防止多个布局嵌套的时候,相同布局的冗余

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</merge>

二、抽象布局include

布局放在一个xml布局文件中,过于臃肿且不方便维护,利用抽象布局include用于将部分布局抽取出来。

方法:

(1)首先创建将要插入的标题栏布局文件title_bar_default.xml

(2)然后用<include>包含进来

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <include layout="@layout/title_bar_default">   
        </include>
    </LinearLayout>
</merge>

抽象布局参见:

http://blog.csdn.net/xyz_lmn/article/details/14524567
三、使用资源文件对资源进行管理

各种资源文件都以xml文件的形式放在resource目录下,以xml文件进行统一管理的好处是防止资源命名冲突和复用:

颜色:colors

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bg_title_bar">#fff8f8f8</color>
</resources>

ID:ids

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <item type="id" name="title_bar">false</item>
</resources>
常量字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SurpriseDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

图片:

放在drable目录下


样式:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

四、标题栏

标题栏要指定一下layout_height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/title_bar"
    android:layout_width="fill_parent"
    android:layout_height="44.0dip"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent" 
        android:background="@color/bg_title_bar">
    </RelativeLayout>
    
</LinearLayout>

五、控件显示状态的控制

这里对ImageView就添加了android:visibility属性来进行控制

         <ImageView
                android:id="@id/iv_setting_notify"
                android:layout_width="9.0dip"
                android:layout_height="9.0dip"
                android:layout_alignParentRight="true"
                android:layout_marginRight="8.0dip"
                android:layout_marginTop="11.0dip"
                android:src="@drawable/ic_message_count"
                android:visibility="visible" />
控制效果如下:


在Android中控件或者布局的可见性android:visibility有3中情况:View.VISIBLE,View.UNVISIBLE,View.GONE。

View.VISIBLE是可见,View.UNVISIBLE是不可见,但是在这种情况下它会占据空间。而View.GONE则

是指该控件的不可见,也不占用系统布局中的空间。

六、selector的使用

在设置背景的时候

七、自定义View的使用


三、一个页面内容纳超出页面视野的布局

一般我们填充一个UI会直接用LinearLayout或者RelativeLayout来做,但如果考虑到页面内容可能比较多,为了避免拉伸和比例变形,可以将一系列布局放在ScrollView内,

形成ListView的效果

三、自定义视图

(1)首先新建自己的View视图类

public class BaseDerivedView extends View {

	public BaseDerivedView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public BaseDerivedView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public BaseDerivedView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

}

(2)在res/values下面新建attr.xml文件,新建自己view资源类型

  <declare-styleable name="BaseDerivedView">
        <attr name="borderwidth" format="dimension" />
        <attr name="bordercolor" format="color" />
        <attr name="border_radius" format="dimension" />
        <attr name="type">
            <enum name="circle" value="0" />
            <enum name="round" value="1" />
        </attr>
    </declare-styleable>



(3)然后在布局文件中直接使用,当然也可以使用自己的图片资源文件(@drawable/default_basebackgroundimg)来指定默认背景

<cn.surprise.view.BaseDerivedView
                        android:id="@+id/iv_first"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="30dp"
                        android:src="@drawable/default_basebackgroudimg" />

四、为布局文件控件统一添加事件监听

(1)首先在布局文件中指定事件的调用函数

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/textView2"
        android:layout_marginBottom="50dp"
        android:text="Button1"
        android:onClick="Click" />

(2)然后在程序中根据View的ID来判断进行相应处理:

public void Click(View v)
	{
		switch(v.getId())
		{
		case R.id.textView1:
			Log.e(TAG,"textView1 Clicked");
			break;
		case R.id.textView2:
			Log.e(TAG,"textView2 Clicked");
			break;
		case R.id.button1:
			Log.e(TAG,"Button1 clicked");
			break;
		case R.id.button2:
			Log.e(TAG,"Button2 clicked");
			break;
		}
	}

五、样式的使用:

(1)在style.xml中定义样式

    <style name="font_big_dark_gray">
        <item name="android:textSize">15.0sp</item>
        <item name="android:textColor">@color/font_dark_gray</item>
    </style>

(2)在button中使用样式

   <Button
                android:id="@+id/button1"
                style="@style/font_big_dark_gray"
                android:layout_width="fill_parent"
                android:layout_height="60.0dip"
                android:background="@drawable/bg_clickable_linearlayout"
                android:drawablePadding="10.0dip"
                android:drawableRight="@drawable/ic_arrow_right"
                android:gravity="center_vertical"
                android:drawableLeft="@drawable/gender_man"
                android:paddingLeft="@dimen/activity_reg_horizontal_margin"
                android:paddingRight="@dimen/activity_reg_horizontal_margin"
                android:text="我的动态" />
(3)其他诸如drawable之类的图片样式,dimen之类的尺寸样式也是同样的使用方式

六、自定义控件属性,TypedArray的使用

http://www.2cto.com/kf/201302/189492.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值