日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

AndroidGUI26:程序中動態(tài)設(shè)定組件的寬度、高度

系統(tǒng) 2972 0
在實(shí)際工作中,我們經(jīng)常需要在程序里面用代碼來控制一些組件的寬度和高度,以適應(yīng)不同分辨率的屏幕。盡管有不同的Layout供我們使用,但很多時候需要通過用代碼設(shè)定組件的大小,以達(dá)到良好的界面視覺效果。

注意:組件和控件是有區(qū)別的。組件對應(yīng)的英文是component,控件對應(yīng)的英文是control;控件是帶有界面的,組件則未必有界面;控件屬于組件,可以說它是帶有界面的組件。比如Button有界面,因此可以說它是控件,也可以說它是組件。LinearLayout沒有界面,因此它不能算是控件,但它卻是組件。本文中由于涉及了帶有和不帶有界面的組件,因此,用組件泛指這兩者。

有些組件,比如Button,可以在程序中用setWidth和setHeight來設(shè)定其大小,這是非常方便的。但有些組件卻沒有這兩個設(shè)定大小的方法,比如ImageButton、Spinner以及LinearLayout等等,那么如何在程序中根據(jù)需要,動態(tài)地設(shè)定他們的大小呢?下面就用實(shí)際的例子來說明這個問題。

1.首先創(chuàng)建一個Android項(xiàng)目:

AndroidGUI26:程序中動態(tài)設(shè)定組件的寬度、高度以及margin等屬性

2.將圖片文件magnifier.png拖入到項(xiàng)目的res/drawable-mdpi文件夾下。mangifier.png的內(nèi)容如下:


3.在strings.xml中,增加如下粗體字代碼。這些代碼,將會被Spinner使用:

<string name= "spin_prompt" >請選擇城市 </string>

<string-array name= "cities" >

<item>北京 </item >

<item>上海 </item >

<item>南京 </item >

<item>烏魯木齊 </item>

<item>哈爾濱 </item>

<item>符拉迪沃斯托克 </item>

</string-array>

4.修改main.xml,使之如下:

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<Buttonandroid:id="@+id/btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<ImageButtonandroid:id="@+id/image_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/magnifier"

/>

<Spinnerandroid:id="@+id/sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:entries="@array/cities"

android:prompt="@string/spin_prompt"

/>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<LinearLayoutandroid:id="@+id/ll_left"

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="left"

>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello"

/>

</LinearLayout>

<LinearLayoutandroid:id="@+id/ll_right"

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="right"

>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Android"

/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

不難看出,mainx.ml有一個Button,一個ImageButton,一個Spinner和兩個EditText。

5.運(yùn)行本項(xiàng)目,得到的結(jié)果如下:

AndroidGUI26:程序中動態(tài)設(shè)定組件的寬度、高度以及margin等屬性

現(xiàn)在假定,我們要:

a)增加Button的高度

b)增加ImageButton的寬度和高度

c)增加Spinner的寬度

d)將包含Hello的EditText靠左,包含Android的EditText靠右

6.修改AdjustControlSize.java的代碼,使之如下:

public class AdjustControlSize extends Activity

{

private Button btn;

private ImageButton imagebtn;

private Spinner sp;

private LinearLayout ll_left;

private LinearLayout ll_right;

private DisplayMetrics dm;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout. main );

// 獲取屏幕尺寸

dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

btn = (Button)findViewById(R.id. btn );

imagebtn = (ImageButton)findViewById(R.id. image_btn );

sp =(Spinner)findViewById(R.id. sp );

ll_left = (LinearLayout)findViewById(R.id. ll_left );

ll_right = (LinearLayout)findViewById(R.id. ll_right );

// 增加Button的高度,可以很方面地通過setHeight方法來實(shí)現(xiàn)。

btn.setHeight(80);

// 但如果要想在代碼中改變某些組件,比如ImageButton、Spinner以及LinearLayout,

// 用setHeight或者setWidth的方式就不行了,因?yàn)檫@些組件中,沒有提供這兩個方法。

// 為此,可以通過LayoutParams這個類(這里我們用LinearLayout.LayoutParams)來實(shí)現(xiàn)這一點(diǎn)。

// 改變imagebtn的寬度和高度均為屏幕寬度的1/4

LinearLayout.LayoutParams imagebtn_params = newLinearLayout.LayoutParams(

LayoutParams. WRAP_CONTENT , LayoutParams. WRAP_CONTENT );

imagebtn_params.height = dm.widthPixels / 4;

imagebtn_params.width = dm.widthPixels / 4;

imagebtn.setLayoutParams(imagebtn_params);

// 設(shè)定sp的寬度為屏幕寬度的2/3

LinearLayout.LayoutParams sp_params = new LinearLayout.LayoutParams(

LayoutParams. WRAP_CONTENT , LayoutParams. WRAP_CONTENT );

sp_params.width = dm.widthPixels * 2 / 3;

sp.setLayoutParams(sp_params);

// 讓兩個EditText分別處于屏幕的左右兩端。

// 在main.xml中,兩個EditText分別處于ll_left和ll_right這兩個LinearLayout中,且

// 處于ll_left的gravity屬性為left,即置于其中的組件靠左;處于ll_right的gravity

// 屬性為right,即置于其中的組件靠右。但是由于這兩個LinearLayout的寬度屬性均為

// wrap_content,所以它們靠在一起了,由此導(dǎo)致了兩個EditText也靠在一起。

// 如果,我們把ll_left和ll_right的寬度能夠設(shè)定為屏幕寬度的一半,那么兩個EditText就

// 會分別處于屏幕的兩端。

LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams(

LayoutParams. WRAP_CONTENT , LayoutParams. WRAP_CONTENT );

ll_params.width = dm.widthPixels / 2;

ll_left.setLayoutParams(ll_params);

ll_right.setLayoutParams(ll_params);

}

}

7.運(yùn)行結(jié)果:

AndroidGUI26:程序中動態(tài)設(shè)定組件的寬度、高度以及margin等屬性

達(dá)到了我們在5中假定的目標(biāo)。

8.總結(jié)

如果在程序中不能用setWidth和setHeight來改變大小的組件,通??梢酝ㄟ^LayoutParams的方式進(jìn)行設(shè)定,正如6中代碼的粗體字部分的代碼那樣。動態(tài)設(shè)定這些組件的margin也是采用類似的方式,比如:

sp_params.width = dm.widthPixels * 2 / 3;

sp_params.leftMargin = 6; // 用這種方式設(shè)定組件的margin

sp.setLayoutParams(sp_params);


AndroidGUI26:程序中動態(tài)設(shè)定組件的寬度、高度以及margin等屬性


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 容城县| 北川| 什邡市| 精河县| 仁怀市| 观塘区| 金沙县| 延庆县| 治多县| 南澳县| 屏东县| 逊克县| 固安县| 平安县| 花垣县| 肃宁县| 永吉县| 墨竹工卡县| 麦盖提县| 铁力市| 巩义市| 陕西省| 思茅市| 湖州市| 莱芜市| 新化县| 华阴市| 博罗县| 宜良县| 苍梧县| 靖江市| 河津市| 申扎县| 福泉市| 洮南市| 英超| 东方市| 嘉禾县| 绥中县| 韩城市| 宿州市|