Android学习自定义View(一)——初识View

MainActivity如下:

package cc.testviewstudy1;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.RelativeLayout;
/**
 * Demo描述:
 * 关于自定义View的学习(一)
 * 
 * 学习资料:
 * http://blog.csdn.net/guolin_blog/article/details/12921889
 * Thank you very much
 *
 */
public class MainActivity extends Activity {
    private RelativeLayout mRelativeLayout;
    private LayoutInflater mLayoutInflater;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		//test1();
		test2();
		test3();
	}
	
	
	//每个Activity由两部分组成.
	//1 title
	//2 contentView
	//我们可以分别设置它们
	private void init(){
		setTitle("This is title");
		setContentView(R.layout.main);
	}
	
	
	
	
	//用button_layout_wrong布局的方式来在View中新加一个Button
	//是不够准确的.因为此时我们是无法通过 android:layout_width和 android:layout_height
	//指定Button的宽和高,最终Button显示的只有wrap_content的大小.
	private void test1(){
		mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
		mLayoutInflater=LayoutInflater.from(MainActivity.this);
		View buttonView=mLayoutInflater.inflate(R.layout.button_layout_wrong, null);
		mRelativeLayout.addView(buttonView);
	}
	
	//怎么解决test1中的问题呢?
	//关键在于android:layout_width和 android:layout_height的理解
	//它指的是的控件在布局中的宽和高所以叫android:layout_width和 android:layout_height
	//而不是叫android:width和 android:height.
	//所以,我们要先把控件放在一个布局里面,然后再给该控件指定宽和高.这样才有效果.
	//如:button_layout_right所示
	private void test2(){
		mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
		mLayoutInflater=LayoutInflater.from(MainActivity.this);
		View buttonView=mLayoutInflater.inflate(R.layout.button_layout_right, null);
		mRelativeLayout.addView(buttonView);
	}
	
	
	
	//继续上面的例子:
	//我们在每次布局的时候不是可以在最外层的布局通过android:layout_width和 android:layout_height
	//来指定该布局的宽和高么然后setContentView()将其显示在屏幕上的么?
	//这个最大的View没有再嵌套一层布局为什么可以指定宽和高呢?
	//这不是和上面的例子冲突了么?
	//其实,不是的.
	//因为在加载每个布局文件xml的时候.不论其根布局是什么,都会将该布局
	//外面嵌套一层FrameLayout.
	//这样就和上面的例子统一了.
	private void test3(){
		mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
		ViewParent viewParent=mRelativeLayout.getParent();
		System.out.println("每个布局文件的最外层的实质是:"+viewParent);
	}
		

}


main.xml如下:

<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"
    android:id="@+id/relativeLayout"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_centerInParent="true"
    />

</RelativeLayout>


button_layout_wrong.xml如下:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="20dp"
    android:textSize="30sp"
    android:text="Button" >

</Button>

button_layout_right.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="Button" 
        android:textSize="30sp">
    </Button>
</RelativeLayout>



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值