導航:首頁 > 操作系統 > 安卓編程如何實現布局

安卓編程如何實現布局

發布時間:2023-12-01 11:35:26

Ⅰ 安卓界面要實現這兩行三列的布局,該怎樣寫代碼

閑著沒事 給你寫了

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

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

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#FFF"

android:orientation="horizontal" >


<Button

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:text="BUTTON1" />


<LinearLayout

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1.5"

android:orientation="vertical" >


<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal" >


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text01"

android:textColor="#000" />


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text01"

android:textColor="#000" />


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text01"

android:textColor="#000" />

</LinearLayout>


<LinearLayout

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal" >


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text02"

android:textColor="#000" />


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text02"

android:textColor="#000" />


<TextView

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="Text02"

android:textColor="#000" />

</LinearLayout>

</LinearLayout>


<Button

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:text="BUTTON2" />


</LinearLayout>

Ⅱ Android 動態布局該如何實現!!

你的問題應該是要動態生成空間吧。
下面的答案可以參考下。
首先要卻這個界面的布局,是AbsoluteLayout,RelativeLayout還是其他,然後就可以再裡面添加控制項了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//確定界面的布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
//創建一個button按鈕
Button btn1 = new Button(this);
btn1.setText(」this is a button」);
btn1.setId(1);
//確定這個控制項的大小和位置
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1 );

}
一個界面可以布置一個布局,可以多個布局一起設計:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//設置界面的布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);

//添加一個AbsoluteLayout子布局,並給這個布局添加一個button
AbsoluteLayout abslayout=new AbsoluteLayout (this);
abslayout.setId(11);
Button btn1 = new Button(this);
btn1.setText(」this is a abslayout button」);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1, lp0 );
//將這個子布局添加到主布局中
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

//再添加一個子布局
RelativeLayout relativeLayout1 = new RelativeLayout(this);
Button btn2 = new Button(this);
btn2.setText(」this is a relativeLayout1 button」);
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout1.addView(btn2 ,lp2);

//將這個布局添加到主布局中
RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW ,11);
relativeLayout.addView(relativeLayout1 ,lp11);
}

閱讀全文

與安卓編程如何實現布局相關的資料

熱點內容
css取消編譯後自動下載 瀏覽:469
做Ug編程需要有加工中心基礎嗎 瀏覽:681
android英語詞典 瀏覽:6
耳機有一邊連不上怎麼辦安卓 瀏覽:155
安卓怎麼玩正版孤膽車神 瀏覽:887
不做程序員還能做什麼 瀏覽:688
excel條件命令 瀏覽:404
autojs源文件加密 瀏覽:183
49演算法虛歲年齡怎麼算 瀏覽:547
qq發圖片壓縮 瀏覽:948
期貨行情手機app編程 瀏覽:53
中山緯創一廠做伺服器是什麼 瀏覽:28
python如何求字典里值的和 瀏覽:287
標准日本語pdf高清版 瀏覽:837
ide導出的二進制編譯文件 瀏覽:970
程序員會引起頸椎病嗎 瀏覽:64
cuda文件編譯 瀏覽:4
為什麼伺服器費用這么高 瀏覽:977
編譯錯誤什麼原因 瀏覽:234
人民文學pdf下載 瀏覽:303