現在就以一個按鈕為例,在xml中設定寬高方式如下
其中"wrap_content"就是照你按鈕內容改變大小,"fill_parent"就是直接佈滿,當然也可以直接以"100px"(100像素)這種方式直接給予固定的大小
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
在runtime中要給予大小,就以上面那button為例,在Activity中的onCreate內加上下面這段
Button myButton = (Button)findViewById(R.id.button);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
myButton.setLayoutParams(params);
直接給予他一個layout的方式來修改就可以順利改變,或者可以採用下面這種方式(兩種效果相同)
Button myButton = (Button)findViewById(R.id.button);
myButton.getLayoutParams().width=150;
myButton.getLayoutParams().height=150;
相關連結
Android Developers
Android Developers - LinearLayout.LayoutParams
Android 取得畫面解析度
沒有留言:
張貼留言