AndroidGUI26:程序中动态设定组件的宽度、高度

系统 2108 0
在实际工作中,我们经常需要在程序里面用代码来控制一些组件的宽度和高度,以适应不同分辨率的屏幕。尽管有不同的Layout供我们使用,但很多时候需要通过用代码设定组件的大小,以达到良好的界面视觉效果。

注意:组件和控件是有区别的。组件对应的英文是component,控件对应的英文是control;控件是带有界面的,组件则未必有界面;控件属于组件,可以说它是带有界面的组件。比如Button有界面,因此可以说它是控件,也可以说它是组件。LinearLayout没有界面,因此它不能算是控件,但它却是组件。本文中由于涉及了带有和不带有界面的组件,因此,用组件泛指这两者。

有些组件,比如Button,可以在程序中用setWidth和setHeight来设定其大小,这是非常方便的。但有些组件却没有这两个设定大小的方法,比如ImageButton、Spinner以及LinearLayout等等,那么如何在程序中根据需要,动态地设定他们的大小呢?下面就用实际的例子来说明这个问题。

1.首先创建一个Android项目:

AndroidGUI26:程序中动态设定组件的宽度、高度以及margin等属性

2.将图片文件magnifier.png拖入到项目的res/drawable-mdpi文件夹下。mangifier.png的内容如下:


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.运行本项目,得到的结果如下:

AndroidGUI26:程序中动态设定组件的宽度、高度以及margin等属性

现在假定,我们要:

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方法来实现。

btn.setHeight(80);

// 但如果要想在代码中改变某些组件,比如ImageButton、Spinner以及LinearLayout,

// 用setHeight或者setWidth的方式就不行了,因为这些组件中,没有提供这两个方法。

// 为此,可以通过LayoutParams这个类(这里我们用LinearLayout.LayoutParams)来实现这一点。

// 改变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);

// 设定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,所以它们靠在一起了,由此导致了两个EditText也靠在一起。

// 如果,我们把ll_left和ll_right的宽度能够设定为屏幕宽度的一半,那么两个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.运行结果:

AndroidGUI26:程序中动态设定组件的宽度、高度以及margin等属性

达到了我们在5中假定的目标。

8.总结

如果在程序中不能用setWidth和setHeight来改变大小的组件,通常可以通过LayoutParams的方式进行设定,正如6中代码的粗体字部分的代码那样。动态设定这些组件的margin也是采用类似的方式,比如:

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

sp_params.leftMargin = 6; // 用这种方式设定组件的margin

sp.setLayoutParams(sp_params);


AndroidGUI26:程序中动态设定组件的宽度、高度以及margin等属性


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论