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>



时间: 2024-09-14 15:54:03

Android学习自定义View(一)——初识View的相关文章

Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解

MainActivity如下: package cc.testviewstudy5; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 自定义ViewGroup及其onMeasure()的理解 * * 参考资料: * 1 http://blog.csdn.net/guolin_blog/article/details/16330267 * 2 http://blog.csdn.net/dawanganba

Android学习自定义View(二)——View和ViewGroup绘制流程以及invalidate()

MainActivity如下: package cc.testviewstudy2; import android.os.Bundle; import android.widget.LinearLayout; import android.app.Activity; /** * Demo描述: * 关于自定义View的学习(二) * * View的绘制流程:onMeasure()-->onLayout()-->onDraw() * * 学习资料: * 1 http://blog.csdn.ne

Android学习自定义View(三)——自绘控件和组合控件

MainActivity如下: package cc.testviewstudy3; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.app.Activity; /** * Demo描述: * 关于自定义View的学习(三) * * 自定义View的实现方式大概可以分为三种: * 自绘控件.组合控件.以及继承控件 * 在此Dem

Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)

MainActivity如下: package cc.testviewstudy4; import java.util.ArrayList; import java.util.HashMap; import cc.testviewstudy4.ListViewSubClass.OnDeleteListener; import android.os.Bundle; import android.widget.SimpleAdapter; import android.app.Activity; /

Android 自定义View是继承view还是viewgroup

问题描述 Android 自定义View是继承view还是viewgroup 我一直对于自定义View一知半解,不知道该怎么去 入手,看到一些自定义的view,一些继承的view,一些是ViewGroup,还有的就是一些自带的控件,怎么去区分我所要的view该继承谁?求郭老师指导一下! 解决方案 看你的具体需求,如果单独一个控件就继承View,如果还要承装其他控件就继承ViewGroup. 解决方案二: Android 自定义View和ViewGroupAndroid 自定义View 和 Vie

Android自定义加载loading view动画组件_Android

在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress  我写写使用步骤  自定义view(CircleProgress )的代码  package com.hysmarthotel.view; import com.hysmarthotel.roomcontrol.R; import com.hysmarthotel.util.EaseInOutCubicInterpolator; import android.

android自定义ListView实现底部View自动隐藏和消失的功能

有这样一个ListView,要求在屏幕底部有一个筛选排序的浮动框: 1.手指下拉隐藏,上滑显示 : 2.如果没做任何操作,2S之后,要自动显示: 3.滑动到最底部,始终显示. 首先看其效果图: 实现上述效果,其实现原理如下: 1.在屏幕顶部固定一个BottomView,XML布局最好使用RelativeLayout(底部的BottomView并不是 ListView的footView,这个是和footView独立的,想想为什么?) 2.然后自定义ListView控件,监听onTouchEvent

Android自定义加载loading view动画组件

在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress )的代码 package com.hysmarthotel.view; import com.hysmarthotel.roomcontrol.R; import com.hysmarthotel.util.EaseInOutCubicInterpolator; import android.ani

Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件(转)

一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: 1.对Android中Window类中的DecorView有所了解 2.对Scroller类实现平滑移动效果 3.自定义ViewGroup的实现 首先来看看效果图吧:      下面现在就来说说这里咱们实现侧滑View的基本思路吧,这里我采用的是自定义一个继承于RelativeLayout的控件叫做