android学习之view的构造

通常来说view可以通过三种方式来构造,第一种就是在在xml直接在xml里面写。。。。

第二种就是通过纯java代码来写,第三种的话layoutflater来实现,在下面的代码,我有详细的说明,并且说了注意事项。

package com.example.helloview2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 1使用布局文件显示界面
		//setContentView(R.layout.activity_main);
		
		
		// 2纯java代码编写界面,并显示
		// 先创建relativelayout
		// 在android里面,context提供给系统应用级的操作,也被认为是静态上下文
		RelativeLayout relativelayout = new RelativeLayout(MainActivity.this);
		// 在xml文件中有几个padding属性,而java中使用一个方法实现
//		relativelayout.setPadding(R.dimen.activity_horizontal_margin,
//				R.dimen.activity_vertical_margin,
//				R.dimen.activity_horizontal_margin,
//				R.dimen.activity_vertical_margin);
		//setpadding设置的是内边框的数据值
		//所以,我们需要获取数据值,也就是在context下获取引用资源
		Resources res=getResources();
		float activity_horizontal_margin=res.getDimension(R.dimen.activity_horizontal_margin);
		float activity_vertical_margin=res.getDimension(R.dimen.activity_vertical_margin);
		relativelayout.setPadding((int)activity_horizontal_margin,
				(int)activity_vertical_margin,
				(int)activity_horizontal_margin,
				(int)activity_vertical_margin);
		//width和height和环境有关
		//创建TextView
		TextView tv=new TextView(this);
		//这里的WRAP_CONTENT常量,他们在xml中设置是,是layout—width这个属性
		//这里的高度和宽度和我们本身的环境有关(父View),所以需要请求当亲布局参数来获取
//		tv.setWidth(tv.getLayoutParams().WRAP_CONTENT);
//		tv.setHeight(tv.getLayoutParams().WRAP_CONTENT);
	
		tv.setText(R.string.hello_world);
		//建立relativelayout和tv的关系
		//这个建立关系的同时只能成立一个
		relativelayout.addView(tv, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		setContentView(relativelayout);
		
		
		
		//3布局注入器
		//获取系统服务
		Context context=MainActivity.this;
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		//我们可以通过xml文件生成View
		//参数1:我们要注入的布局文件;参数二:我们通过布局文件生成的View对象,要放在那个父View;
		View view=inflater.inflate(R.layout.activity_main, null);
		if(view instanceof RelativeLayout){
			System.out.println("view 是从relative生成的");
		}
		//setContentView(view);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeArray 是 Android 中的一个特殊的资源类型,用于在 XML 中声明自定义 View 的属性。使用 TypeArray 可以方便地在 XML 布局中指定 View 的属性,而不需要在 Java 代码中进行硬编码。 使用 TypeArray 的步骤如下: 1. 在 res/values/attrs.xml 文件中定义自定义 View 的属性。 ```xml <resources> <declare-styleable name="MyCustomView"> <attr name="customAttr1" format="integer" /> <attr name="customAttr2" format="string" /> <attr name="customAttr3" format="boolean" /> </declare-styleable> </resources> ``` 2. 在自定义 View构造函数中获取 TypedArray 对象,并从中获取属性值。 ```java public class MyCustomView extends View { private int customAttr1; private String customAttr2; private boolean customAttr3; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); customAttr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0); customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2); customAttr3 = a.getBoolean(R.styleable.MyCustomView_customAttr3, false); a.recycle(); } } ``` 在上面的代码中,`context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)` 用于获取 TypedArray 对象,`R.styleable.MyCustomView` 是在 attrs.xml 文件中定义的自定义属性集合,`a.getInt()`、`a.getString()`、`a.getBoolean()` 用于从 TypedArray 对象中获取属性值,最后需要调用 `a.recycle()` 来回收 TypedArray 对象。 3. 在 XML 布局中使用自定义 View,并设置属性值。 ```xml <com.example.MyCustomView android:layout_width="match_parent" android:layout_height="wrap_content" app:customAttr1="123" app:customAttr2="hello" app:customAttr3="true" /> ``` 在上面的代码中,`app:customAttr1`、`app:customAttr2`、`app:customAttr3` 是在 attrs.xml 文件中定义的自定义属性名,可以在 XML 布局中使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值