天天看點

Android 布局學習筆記

基本布局方式:LineearLayout (線性布局)

              RelativeLayout (相對布局)

              TableLayout (表格布局)

              FrameLayout (幀布局)

參考文檔:

file://D:\FProjects\FAndroid\FTools\androidSDK tools\android-sdk\docs\guide\topics\ui\layout\

LineearLayout (線性布局):按照垂直或水準方向依次擺放控件

RelativeLayout (相對布局):依據與父元素或同級元素的相對位置擺放控件

TableLayout (表格布局):與網頁表格布局類似

FrameLayout (幀布局):後面的控件疊加顯示在前面的控件之上

所有可視控件都可以使用的XML屬性:

Android:id :在R檔案id下新增加名字為idname的資源,以後可以用名稱idname來應用該控件。

android:id="@+id/idname"

在代碼中使用

overlayView=(GestureOverlayView)this.findViewById(R.id.idname)

就可以找到該控件并賦給overlayView。

Android:layout_width

Android:layout_height

設定寬度和高度,機關建議用dp.

有兩個經常用到的值:match_parent:填滿父元素

wrap_content:包裹内容

Android:layout_marginLeft:設定左邊距

Android:layout_marginRight:設定右邊距

Android:layout_marginTop:設定上邊距

Android:layout_marginBottom:設定下邊距

Android:layout_margin:相當于上面四個一起使用,此标簽比上面四個優先級高,設定完此标簽,上面四個就失效了。

Android:padingLeft

Android:padingRight

Android:padingTop

Android:padingBottom

Android:pading  :用法同上,優先級比上面四個要高。設定内邊距的标簽。

Android:layout_weight:取值為任意正整數。為零表示控件保持原大小。假設有四個控件,第一個Android:layout_weight設為0,其餘三個分别為1,2,3.則第一個按原大小顯示,剩餘空間被分成1+2+3=6等份,第二個占1份,第三個占2份,第四個占3份。

Android:gravity 控件中内容的位置

Android:layout_gravity: 控件的位置

可取的值:center_horizontal 水準居中,center_vertical 垂直居中,

Center 在水準和垂直都居中,left,right,top,bottom.

Android:visible 決定目前控件是否可見

可取的值:visible 可見,invisible 不可見,但是保留位置,相當于控件透明。,gone 不可見也不保留位置。

Android:background 設定背景色或者背景圖像

Android:background=”#FF00FF” 設定背景色

Android:background=”@drawable/background” 設定背景圖像

Android:onClick 指定單擊該控件後響應該事件的方法名稱。

Android:clickable 是否接受單擊事件

Android:longClickable  是否接受長按單擊事件

Android:focusable:是否可以将焦點移動到目前控件,使用滑鼠或鍵盤

Android:focusableInTouchMode 是否可以觸摸獲得焦點,當為true時,第一次單擊會得到焦點,第二次單擊才會觸發單擊事件。

控件介紹:

1.TextView:

顯示文本的控件:

使用方法設定TextView的屬性:

TextView.setText

TextView.setTextColor

TextView.setTextBackGround

TextView.setTextSize

TextView.setLineSpacing(50,1.2f)  設定為50dp和1.2倍行間距較大的那一個值。

TextView.setEllipsize(TextUtils.TruncateAt.START) 在開始加省略号

使用XML屬性設定屬性:

設定行間距:

Android:lineSpacingExtra=”20dp”設定行間距為20dp

Android:lineSpacingMultiplier=”1.8” 設定行間距為行寬的1.8倍

設定省略号:

Android:ellipsize=”start|middle|end” 在前|中|後加省略号

文本水準滾動顯示:

Android:ellipse=”marquee”

Android:marqueeRepeatLimit=”marquee_forever”  設定循環次數

Android:fousable=”true”

必須三項同時設定才行。

文本垂直滾動顯示:

Android:scollbars=”vertical”

Android:text=”需要一段較長的文本”

Android:scollbarStyle=”outsideOverlay|insideOverlay”滾動條在右側顯示|滾動條在右側文本上顯示

Android:scollbarFadeDuration=”200”滾動條從出現到消失200ms

基本用法

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:textSize="16sp"

    android:textColor="#000099"  文本顔色

        android:id="@+id/textView"

        android:text="@string/phone"  顯示的文本

    />

在代碼中使用TextView.setText可以改變要顯示的文本

顯示富文本:

<font>設定顔色和字型

<big>設定大号字

<small>設定小号字

<i>斜體

<b>粗體

<tt>等寬字型

<br>換行

<p>換兩行

<a>連結位址

<img>插入圖像

富文本不能簡單的使用TextView.setText改變顯示的内容。具體做法如下:

TextView textView =(TextView) findViewById(R.id.textview);

String  html = "<font color='red'>十月一放假七天</font><br>";

html += "<font color='#00FF00'><big><i>十月一放假七天</i></big></font><p>";

html += "<font color='white' ><tt><b><big><u>十月一放假七天</u></big></b></tt></font><p>";

html += "<big><a href='http://www.baidu.com'>百度一下,你就知道</a></big>";

//此時html裡面存的是一串字元

//将字元串轉成CharSequence對象

CharSequence charSequence = Html.fromHtml(html);

//為TextView控件設定要顯示的富文本

textView.setText(charSequence);

//設定單擊連結時調用浏覽器

textView.setMovementMethod(LinkMovementMethod.getInstance());

Android 布局學習筆記

顯示表情和文字:

使用img标簽可以引入圖像,src指定要顯示的圖像,但是系統不會自動解析src指定的路徑,需要使用者使用ImageGetter對象的getDrawable方法。Src值的具體含義,需要早getDrawable方法中确定。

String html2 = "圖像1<img src='image1'>";

html2 += "圖像2<img src='image2'>";

html2 += "圖像3<img src='image3'>";

html2 += "圖像4<a href='http://www.baidu.com'><img src='image4'></a>";

CharSequence charSequence2 = Html.fromHtml(html2,new ImageGetter(){

public Drawable getDrawable(String source){

//裝載圖像資源

Drawable drawable = getResources().getDrawable(getResouseId(source));

//如果是image4,按原來大小的50%顯示

if(source.equals("image4")){

drawable.setBounds(0,0,drawable.getIntrinsicWidth()/2,drawable.getIntrinsicHeight()/2);

}

//其餘按原來大小顯示

else{

//setBounds設定圖像的顯示區域,預設是0,也就是不會顯示出來,是以必須設定。

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

}

return drawable;

}

}, null);

textView.setText(charSequence2);

textView.setMovementMethod(LinkMovementMethod.getInstance());

//自定義擷取資源的方法,利用反射技術将     檔案名  映射成資源  ID

public  int getResouseId(String name){

try{

//根據圖像資源的檔案名得到Field對象

Field filed = R.drawable.class.getField(name);

//取得并傳回資源ID字段的值

return Integer.parseInt(filed.get(null).toString()) ;

}

catch(Exception e){

}

return 0;

}

Android 布局學習筆記

單擊圖像4的圖像可以打開百度:

Android 布局學習筆記

此外,還可以自定義單擊連接配接打開的activity。

為指定文本添加前景色和背景色

Android 布局學習筆記

public class ColorSpan extends CharacterStyle{

private int mTextColor;

private int mBackgroundColor;

public ColorSpan(int textColor,int backgroundColor){

mTextColor = textColor;

mBackgroundColor = backgroundColor;

}

public void updateDrawState(TextPaint tp) {

              tp.bgColor = mBackgroundColor;

              tp.setColor(mTextColor);

}

}

String html3 = "1234567890abcdefghijk";

//第一步,将字元串轉換成SpannableString對象

SpannableString spannableString = new SpannableString(html3);

//第二部,确定子串的開始和結束位置

int start = 10;

int end = 16;

//第三步,建立BackgroundColorSpan對象

BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);

//第四步,使用setSpan方法将指定子串轉換成backgroundColorSpan對象

spannableString.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

ColorSpan colorSpan = new ColorSpan(Color.RED, Color.BLUE);

spannableString.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

//第五步,用spannableString對象設定TextView對象

textView.setText(spannableString);

還可以在布局檔案中指定顯示富文本:

<string name="hello_world"><a href="tel:12345678">call me</a></string>

帶邊框的TextView:

方法1:編寫一個繼承TextView的類,實作onDraw方法。

方法2:用9-patch做背景圖