排版java
若是在一个layout中有几个widget,最后一个widget采用fill_parent,这将填满剩余的空间。若是某一个widget(非最后一个)采用fill_parent,则后面的widget将没法显示。从某个意义上fill_parent能够理解为父layout剩余的全部空间。android
Eclipse中的提示功能git
咱们在使用eclipse时候,在编译XML,会自动由提示,可能会由于咱们按了一下鼠标或者其余方式,提示消失,能够采用Atl+/的方式,启动提示。api
TextView(Label)eclipse
在Andriod里面是textview,在XML里面有下的一些属性ide
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"-->
/>this
在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.android.com/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开发相关文章