导航:首页 > 操作系统 > 安卓编程如何实现布局

安卓编程如何实现布局

发布时间: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);
}

阅读全文

与安卓编程如何实现布局相关的资料

热点内容
什么java编译器支持中文 浏览:563
香港服务器如何做代理 浏览:201
pdf写入 浏览:986
高尔夫电台怎么添加到文件夹 浏览:239
四川麻将一般下哪个app 浏览:864
反编译exe脚本 浏览:462
源码文件夹怎么编译到固件中 浏览:912
ERp打印服务器错误怎么弄 浏览:113
蚌端口u盘加密软件有哪些 浏览:180
前端如何认证服务器 浏览:556
linux切换db2用户命令 浏览:308
相片如何用电解压 浏览:908
硕士程序员去学校当老师 浏览:122
pythonstr提取到字典 浏览:820
程序员那么可爱有人看上陆漓了 浏览:878
php正则提取图片 浏览:105
pythonlinuxdjango 浏览:565
php中文返回乱码 浏览:91
宿舍装的电信怎么加密 浏览:747
为什么压缩文件解压后变少了 浏览:428