当前位置:首页 » 安卓系统 » android控件初始化

android控件初始化

发布时间: 2024-10-16 03:52:50

① 从源码中浅析Android中怎么利用attrs和styles定义控件

1.attrs.xml:
我们知道Android的源码中有attrs.xml这个文件,这个文件实际上定义了所有的控件的属性,就是我们在布局文件中设置的各类属性
你可以找到attrs.xml这个文件,打开它,全选,右键->Show In->OutLine。可以看到整个文件的解构

我们大概可以看出里面是Android中的各种属性的声明,比如textStyle这个属性是这样定义的:
java代码
<!-- Default text typeface style. -->
<attr name="textStyle">
<flag name="normal" value="0" />
<flag name="bold" value="1" />
<flag name="italic" value="2" />
</attr>
那么现在你知道,我们在写android:textStyle的时候为什么会出现normal,bold和italic这3个东西了吧,就是定义在这个地方。
再看看textColor:
Java代码
<!-- Color of text (usually same as colorForeground). -->
<attr name="textColor" format="reference|color" />
format的意思是说:这个textColor可以以两种方式设置,要么是关联一个值,要么是直接设置一个颜色的RGB值,这个不难理解,因为我们可以平时也这样做过。

也就是说我们平时在布局文件中所使用的各类控件的属性都定义在这里面,那么这个文件,除了定义这些属性外还定义了各种具体的组件,比如TextView,Button,SeekBar等所具有的各种特有的属性
比如SeekBar:

Java代码
<declare-styleable name="SeekBar">
<!-- Draws the thumb on a seekbar. -->
<attr name="thumb" format="reference" />
<!-- An offset for the thumb that allows it to extend out of the range of the track. -->
<attr name="thumbOffset" format="dimension" />
</declare-styleable>
也许你会问SeekBar的background,等属性怎么没有看到?这是因为Android中几乎所有的组件都是从View中继承下来的,SeekBar自然也不例外,而background这个属性几乎每个控件都有,因此被定义到了View中,你可以在declare-styleable:View中找到它。

总结下,也就是说attrs.xml这个文件定义了布局文件中的各种属性attr:***,以及每种控件特有的属性declare-styleable:***

2.styles.xml:
刚才的attrs.xml定义的是组件的属性,现在要说的style则是针对这些属性所设置的值,一些默认的值。

这个是SeekBar的样式,我们可以看到,这里面设置了一个SeekBar的默认的样式,即为attrs.xml文件中的各种属性设置初始值
Java代码
<style name="Widget.SeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
<item name="android:thumb">@android:drawable/seek_thumb</item>
<item name="android:thumbOffset">8dip</item>
<item name="android:focusable">true</item>
</style>
这个是Button的样式:
Java代码
<style name="Widget.Button">
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
<item name="android:textColor">@android:color/primary_text_light</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>

有了属性和值,但是这些东西是如何关联到一起的呢?它们如何被android的framework层所识别呢?

3.组件的源码
我们看下TextView的源码:
Java代码
public TextView(Context context) {
this(context, null);
}//这个构造器用来给用户调用,比如new TextView(this);

public TextView(Context context,
AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

public TextView(Context context,
AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);//为用户自定义的TextView设置默认的style
mText = "";

//设置画笔
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = getResources().getDisplayMetrics().density;
mTextPaint.setCompatibilityScaling(
getResources().getCompatibilityInfo().applicationScale);

mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHighlightPaint.setCompatibilityScaling(
getResources().getCompatibilityInfo().applicationScale);

mMovement = getDefaultMovementMethod();
mTransformation = null;

//attrs中包含了这个TextView控件在布局文件中定义的属性,比如android:background,android:layout_width等
//com.android.internal.R.styleable.TextView中包含了TextView中的针对attrs中的属性的默认的值
//也就是说这个地方能够将布局文件中设置的属性获取出来,保存到一个TypeArray中,为这个控件初始化各个属性
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);

int textColorHighlight = 0;
ColorStateList textColor = null;
ColorStateList textColorHint = null;
ColorStateList textColorLink = null;
int textSize = 15;
int typefaceIndex = -1;
int styleIndex = -1;

/*
* Look the appearance up without checking first if it exists because
* almost every TextView has one and it greatly simplifies the logic
* to be able to parse the appearance first and then let specific tags
* for this View override it.
*/
TypedArray appearance = null;
//TextView_textAppearance不太了解为什么要这样做?难道是为了设置TextView的一些默认的属性?
int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
if (ap != -1) {
appearance = context.obtainStyledAttributes(ap,
com.android.internal.R.styleable.
TextAppearance);
}
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);

switch (attr) {
case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
textColorHighlight = appearance.getColor(attr, textColorHighlight);
break;

case com.android.internal.R.styleable.TextAppearance_textColor:
textColor = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textColorHint:
textColorHint = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textColorLink:
textColorLink = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textSize:
textSize = appearance.getDimensionPixelSize(attr, textSize);
break;

case com.android.internal.R.styleable.TextAppearance_typeface:
typefaceIndex = appearance.getInt(attr, -1);
break;

case com.android.internal.R.styleable.TextAppearance_textStyle:
styleIndex = appearance.getInt(attr, -1);
break;
}
}

appearance.recycle();
}
//各类属性
boolean editable = getDefaultEditable();
CharSequence inputMethod = null;
int numeric = 0;
CharSequence digits = null;
boolean phone = false;
boolean autotext = false;
int autocap = -1;
int buffertype = 0;
boolean selectallonfocus = false;
Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
drawableBottom = null;
int drawablePadding = 0;
int ellipsize = -1;
boolean singleLine = false;
int maxlength = -1;
CharSequence text = "";
CharSequence hint = null;
int shadowcolor = 0;
float dx = 0, dy = 0, r = 0;
boolean password = false;
int inputType = EditorInfo.TYPE_NULL;

int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
//通过switch语句将用户设置的,以及默认的属性读取出来并初始化
switch (attr) {
case com.android.internal.R.styleable.TextView_editable:
editable = a.getBoolean(attr, editable);
break;

case com.android.internal.R.styleable.TextView_inputMethod:
inputMethod = a.getText(attr);
break;

case com.android.internal.R.styleable.TextView_numeric:
numeric = a.getInt(attr, numeric);
break;

//更多的case语句...

case com.android.internal.R.styleable.TextView_textSize:
textSize = a.getDimensionPixelSize(attr, textSize);//设置当前用户所设置的字体大小
break;

case com.android.internal.R.styleable.TextView_typeface:
typefaceIndex = a.getInt(attr, typefaceIndex);
break;
//更多的case语句...
}

通过上面的代码大概可以知道,每个组件基本都有3个构造器,其中只传递一个Context上下文的那个构造器一般用来在java代码中实例化使用。
比如你可以
Java代码
TextView tv = new TextView(context);
来实例化一个组件。

最终调用的是第3个构造器
Java代码
public TextView(Context context,
AttributeSet attrs,
int defStyle)

在这个构造器中为你设置了默认的属性attrs和值styles。关键不在这里,而是后面通过使用下面的代码
Java代码
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
来将属性和值获取出来,放到一个TypeArray中,然后再利用一个switch语句将里面的值取出来。再利用这些值来初始化各个属性。这个View最终利用这些属性将这个控件绘制出来。
如果你在布局文件中定义的一个View的话,那么你定义的值,会被传递给构造器中的attrs和styles。也是利用同样的方式来获取出你定义的值,并根据你定义的值来绘制你想要的控件。
再比如其实Button和EditText都是继承自TextView。看上去两个控件似乎差异很大,其实不然。Button的源码其实相比TextView变化的只是style而已:

② android控件大全(详细介绍常用的UI控件及使用方法)

Android控件是Android应用中最基本的组成部分之一,它们可以帮助我们构建用户界面并实现应用程序的各种功能。在本文中,我们将介绍一些常用的AndroidUI控件及其使用方法。

TextView

TextView是Android中最基本的控件之一,它用于显示文本。要在应用程序中使用TextView,可以按照以下步骤进行操作:

1.在XML布局文件中添加TextView控件:

```

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="HelloWorld!"/>

```

2.在Java代码中获取TextView控件的引用:

```

TextViewtextView=findViewById(R.id.textView);

```

3.设置TextView控件的文本内容:

```

textView.setText("HelloAndroid!");

```

Button

Button是Android中常用的控件之一,它用于响应用户的点击事件。要在应用程序中使用Button,可以按照以下步骤进行操作:

1.在XML布局文件中添加Button控件:

```

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Clickme!"/>

```

2.在Java代码中获取Button控件的引用:

```

Buttonbutton=findViewById(R.id.button);

```

3.设置Button控件的点击事件:

```

button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

//在这里编写点击事件的处理代码

}

});

```

ImageView

ImageView是Android中用于显示图片的控件之一,它可以显示来自资源文件或网络的图片。要在应用程序中使用ImageView,可以按照以下步骤进行操作:

1.在XML布局文件中添加ImageView控件:

```

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my_image"/>

```

2.在Java代码中获取ImageView控件的引用:

```

ImageViewimageView=findViewById(R.id.imageView);

```

3.设置ImageView控件的图片源:

```

imageView.setImageResource(R.drawable.my_image);

```

EditText

EditText是Android中用于输入文本的控件之一,它可以让用户输入文本并将其发送到应用程序中。要在应用程序中使用EditText,可以按照以下步骤进行操作:

1.在XML布局文件中添加EditText控件:

```

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Entertexthere"/>

```

2.在Java代码中获取EditText控件的引用:

```

EditTexteditText=findViewById(R.id.editText);

```

3.获取EditText控件中的文本内容:

```

Stringtext=editText.getText().toString();

```

CheckBox

CheckBox是Android中用于选择一个或多个选项的控件之一,它可以让用户从多个选项中进行选择。要在应用程序中使用CheckBox,可以按照以下步骤进行操作:

1.在XML布局文件中添加CheckBox控件:

```

android:id="@+id/checkBox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Checkme!"/>

```

2.在Java代码中获取CheckBox控件的引用:

```

CheckBoxcheckBox=findViewById(R.id.checkBox);

```

3.获取CheckBox控件的选中状态:

```

booleanisChecked=checkBox.isChecked();

```

RadioButton

RadioButton是Android中用于选择一个选项的控件之一,它可以让用户从多个选项中选择一个。要在应用程序中使用RadioButton,可以按照以下步骤进行操作:

1.在XML布局文件中添加RadioButton控件:

```

android:id="@+id/radioButton1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option1"/>

android:id="@+id/radioButton2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option2"/>

```

2.在Java代码中获取RadioButton控件的引用:

```

RadioButtonradioButton1=findViewById(R.id.radioButton1);

RadioButtonradioButton2=findViewById(R.id.radioButton2);

```

3.获取RadioButton控件的选中状态:

```

booleanisChecked1=radioButton1.isChecked();

booleanisChecked2=radioButton2.isChecked();

```

Spinner

Spinner是Android中用于选择一个选项的控件之一,它可以让用户从多个选项中选择一个。要在应用程序中使用Spinner,可以按照以下步骤进行操作:

1.在XML布局文件中添加Spinner控件:

```

android:id="@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:entries="@array/my_options"/>

```

2.在Java代码中获取Spinner控件的引用:

```

Spinnerspinner=findViewById(R.id.spinner);

```

3.获取Spinner控件的选中项:

```

StringselectedItem=spinner.getSelectedItem().toString();

```

Conclusion

本文介绍了一些常用的AndroidUI控件及其使用方法,包括TextView、Button、ImageView、EditText、CheckBox、RadioButton和Spinner。通过学习这些控件,您可以更好地构建Android应用程序的用户界面,并实现各种功能。希望这篇文章对您有所帮助!

③ android中xml中有些控件的属性里面有 "app:.." ,此处的app:是什么意思和一般的android:有什么区别

区别是:这两个是声明的不同的命名空间,android的是系统的,app是自定义的。


Android自定义控件的属性,在xml中使用自己自定义的attr的时候,其中有一步就是要自定义一个xml的命名空间后然后再给自定义属性赋值,现在发现不知道什么时候开始Android把这个改了,现在发现可以统一用
xmlns:app="http://schemas.android.com/apk/res-auto"
而不是原来的:
xmlns:app="http://schemas.android.com/apk/App的Package名"
还有人提到在作为lib被使用的时候,也应该用res-auto

所以说区别就是如果你http://schemas.android.com/apk/后面写的是res/包名,那就是相关包名下的自定义属性,而res-auto就是所有的自定义包名。

④ Edraw Max怎么制作Android面板控件

Android手机是经常使用的手机,随着Android系统越来越受到欢迎,使用越来越广泛。Edraw Max设计软件自带很多Android UI模型,可以利用这些设计出不同的Android模型,操作如下:

1、双击打开Edraw Max设计软件,新建线框图,如下图所示:

2、在预定义模板和例子中选择线框图, 选择Android UI,点击创建图标,如下图所示:

3、新建空白的画布,左侧有Android UI、矢量图标和触摸手势,如下图所示:

4、在Android UI中的安卓手机框,拖曳一个垂直方向的手机框,如下图所示:

5、找一个状态栏2,拖曳到画布上,并调整手机适应度,如下图所示:

6、找到键盘控制,拖曳一个到手机框下方,使其适应手机框,如下图所示:

⑤ Android之自定义控件

一、简单自定义控件MyButton



每一个控件都是一个java类,有对应的代码,只要你能正确的编写java代码,那么电脑培训发现可以创造出符合你需求的控件,即自定义控件。


1.通过继承的方式,创建自定义控件


通过继承一个现有的控件,覆盖其界面的呈现


通过继承一个包含若干子控件的布局


通过继承一个现有的控件,覆盖某个响应事件


继承一个View来完整自定义一个心控件


2.使你的自定义控件继承自某个最接近的Android控件,必须是public


一般都会调用父类的构造方法,注意一般有三个构造方法


覆盖原来控件的方法,注意是否要再调用super中的方法


在XML中以类全名的方式引用此控件


二、复杂自定义控件MyLogin


需要设计包含一组控件的自定义控件就需要用到复杂的自定义控件


1)使得你的自定义控件继承自某个接近的布局


2)正确的实现构造方法:构造方法中实例化目标布局,同时查找到各个子布局


3)添加相应的响应代码来修改属性,使得外部能访问布局中的子控件


4)在XML中以类全名的方式引用此控件,完整的包名+类名。


热点内容
安卓转苹果为什么要付99块钱 发布:2024-10-16 16:43:57 浏览:120
withsql多个语句 发布:2024-10-16 16:43:56 浏览:337
必学编程语言 发布:2024-10-16 16:38:09 浏览:879
精灵盛典安卓怎么升级 发布:2024-10-16 16:38:03 浏览:342
安卓怎么用更久 发布:2024-10-16 16:29:30 浏览:731
撬装压缩机 发布:2024-10-16 16:16:54 浏览:315
电文编译程序 发布:2024-10-16 16:15:56 浏览:638
鹿鼎记2ftp 发布:2024-10-16 16:15:45 浏览:153
如何把手机做成云服务器 发布:2024-10-16 16:11:54 浏览:362
我的世界服务器停止下雪 发布:2024-10-16 15:59:13 浏览:61