排版 如果在一个layout中有几个widget,最后一个widget采用fill_parent,这将填满剩余的空间。如果某一个widget(非最后一个)采用fill_parent,则后面的widget将无法显示。从某个意义上fill_parent可以理解为父layout剩余的所有空间。 Eclipse中的提示功能 我们在使用eclipse时候,在编译XML,会自动由提示,可能会因为我们按了一下鼠标或者其他方式,提示消失,可以采用Atl+/的方式,启动提示。 TextView(Label) 在Andriod里面是textview,在XML里面有下的一些属性 android:typeface 字体monospace android:textStyle bold italic bold|italic android:textColor #FF0000,red android:textSize 例如"25px",在尺寸方式,有时我们使用px,有时使用dip。px指的像素,dip指的是一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px,采用dip,我们可以无须考虑像素是否密集,而获取我们期待的大小,因此推荐使用dip。 android:gravity 内容的位子,center,left,right等等 android:background 背景色,采用RGB方式 android:singleLine 值为flase或者ture,如果false这允许多行。
Button 这是TextView的subclass,Lable的所有,也适用于Button。在Andriod学习笔记(四):不使用IDE采用命令行中我们给出了一个按键出发的例子,利用XML,可以更为简单。 <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="buttonClickAction" <!-- 格式为andriod:onClick="method_name"--> />
在java source code中,我们在类中无须implement interface,可以直接在public void <method_name>(View v)这样处理。如下: ...... import android.view.View;
public class Activity01 extends Activity { public void buttonClickAction(View button){ ... ... } }
ImageView和ImageButton 这个对应的是TextView和Button,只不过是Image,下面是一个ImageView的例子: <ImageView android:id="@+id/mylanscape" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:src="@drawable/hdrlandscape" <!--这里是图片的来源,也可以通过setImageURI()指定URI,我们在res/drawable-hdpi中放置了一个图片hdrlandscape.jpg,通过android:src可实现加载--> />
EditText 下面是一个例子: <EditText android:id="@+id/myfield" android:layout_width="fill_parent" android:layout_height="fill_parent" android:singleLine="false" <!-- 这里表示多行--> <!-- android:autoText 自动进行拼写检验 addroid:capitalize 单词中第一个字母自动为大写,这对于名词,城市的EditText很有帮助 andriod:digits 只允许数字--> />
CheckBox CheckBox和RadioBox都是从CompoundButton中继承的,而CompoundButton是继承TextView。在XML中如下定义: <CheckBox android:id="@+id/mycheckbox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This checkbox is:uncheck" />
常用的checkcbox函数有:isChecked(),setChecked(),toggle()(改变状态,如是checked的变成unchecked,如果是unchecked变为checked。)。如果CheckBox的状态发生更改,需要在程序中进行触发方法处理。如下: public class HelloAndriod extends Activity implements CompoundButton.OnCheckedChangeListener{
private CheckBox mycheckbox = null; @Override public void onCreate(Bundle savedInstanceState){ ... ... mycheckbox = (CheckBox)findViewById(R.id.mycheckbox); mycheckbox.setOnCheckedChangeListener(this); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ mycheckbox.setText("This checkbox is : " + (isChecked ? "checked":"unchcked")); } }
RadioBox RadioBox处理外观上于CheckBox不一样外,RadioBox通常会组成一个Group,在Group中只能有一个RadioBox处于checked状态。在XML中如下处理: <RadioGroup android:id="@+id/myradiogroup" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text ="Radio Text One" /> <RadioButton android:id="@+id/radio2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text ="Radio Text Two" /> <RadioButton android:id="@+id/radio3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text ="Radio Text Three" /> </RadioGroup>
我们更常操作RadioGroup,常见的方法包括有check(),例如roup.check(R.id.radio1)),将R.id.radio1对应的radiobutton选上;clearCheck(),清楚所有的选择,全部都是unchecked;getCheckedRadioButtonId(),获取选上的radiobutton的ID,无则返回-1。在下面的例子中,我们在之前的checkbox的例子上增加radiobox public class HelloAndriod extends Activity implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener{ private RadioGroup myradiogroup = null; public void onCreate(Bundle savedInstanceState){ ... ... myradiogroup = (RadioGroup)findViewById(R.id.myradiogroup); myradiogroup.setOnCheckedChangeListener(this); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ mycheckbox.setText("This checkbox is : " + (isChecked ? "checked":"unchcked")); }
public void onCheckedChanged(RadioGroup group, int checkedId){ int radioId = myradiogroup.getCheckedRadioButtonId(); if(radioId < 0 ) myTextView.setText("No Radio Button is selected"); else{ RadioButton rb = (RadioButton)group.findViewById(radioId); myTextView.setText("radio button: " + rb.getText()); } } }
View 上面的widget都是View,可以在XML中使用View的特性。 例如:android:visibility="invisible",这使得widget不可见,但是保留其所占的位置,如果是"gone",则不保留位置。 和颜色相关的android:background,可以是具体的颜色,例如android:background="#0000ff",也可以是图片,例如android:background="@drawable/hdrlandscape",但是图片的话,ckeckbox的占用位置需考虑图片的大小。TextView以及其继承类可以使用android:textColor="#00ff00"来指定文本的颜色,处此之外,还可以采用ColorStateList的方式来设定不同情况下text的颜色。 我们在res/layout/目录下新增一个Android XML文件button_color.xml对我们上面的button在不同状态下的颜色进行描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas./apk/res/android"> <item android:color="#00ff00" android:state_focused="true"/> <item android:color="#ff0000" android:state_pressed="true" /> <item android:color="#0000ff" android:state_pressed="false" /> </selector> 这些选择从上至下是具有优先级别的,例如我们将state_focused放置在最后,并不起作用,因为会先执行了state_pressed="false"的颜色。相关的状态有state_pressed, button_color, state_focused, state_selected, state_active, state_checkable, state_checked, state_enabled, state_window_focused。 然后我们main.xml,对相关的widget,增加:android:textColor="@layout/button_color"
由于继承View,可以使用View的方法,与UI相关的:setEnabled(), isEnabled(),requestFocus(),isFocused 在容器中获取widget中实例相关的有:getParent()-获得父容器或者父widget,findViewById(),getRootView(),这些在XML中都很好理解。 相关链接: 我的Android开发相关文章
|