Android學習筆記(六):xml和widget

來源:互聯網
上載者:User

排版

如果在一個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.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開發相關文章

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.