导航:首页 > 编程语言 > android代码添加布局

android代码添加布局

发布时间:2023-07-01 10:01:49

1. Android中怎么用纯代码编写布局

一、用RelativeLayout进行纯代码布局的理论基础
1、RelativeLayout,顾名思义,就是以“相对”位置/对齐 为基础的布局方式。
2、android.widget.RelativeLayout 有个继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例
调用RelativeLayout.addView 就可以实现“相对布局”。 android.widget.RelativeLayout.LayoutParams 有一个构造函数:
RelativeLayout.LayoutParams(int w, int h),参数指定了子 View 的宽度和高度,这一点和其父类是一样的。而实现相对布局的关
键在它的 两个 addRule 方法上。anchor 参数指定可以是View 的 id(“相对于谁”)、RelativeLayout.TRUE(启用某种对齐方式) 或者
是-1(应用于某些不需要 anchor 的 verb);AddRule 方法的 verb 参数指定相对的“动作”(以下常量均定义于
android.widget.RelativeLayout中,为了简便不给出其全名):
3、ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底边/左边/右边/顶边 和 anchor 指定的 View 的
底边/左边/右边/顶边 对齐。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、
ALIGN_WITH_PARENT_TOP : 和上面一组常量类似,只不过不需要再指定 anchor, 其 anchor 自动为 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 为 TRUE,在 Parent 中 水平居中/水平
和垂直均居中/垂直居中。
POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位于 anchor 指定的 View
的上边/下边/左边/右边。

2. android如何在代码里设置布局

java">//定义一个线性布局
LindearLayouttp=newLindearLayout(this);
//定义一个布局参数类(用于定义Button在线性布局中的参数)
LayoutParamsltp=newLayoutParams(LayoutParams.WARP_CONTENT,LayoutParams.WARP_CONTENT);
Buttonlbt=newButton(this);
tp.addView(lbt,ltp);//将内Button加入到线性布局中。

//但是容不推荐代码里实现,因为android是MVC开发模式,数据操作,UI,代码等实现都是分开写的,这样写的好处你多编点代码就会体会到了。虽然靠纯代码甚至是在一个activity中都可能完成一个app的编写,但是这样就完全没了可维护性了

3. android 如何利用java代码 在一个xml布局中插入另一个xml布局

Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。

先看下我们要实现的整体界面:

一、未使用Include时

通常情况下,我们直接就能写出布局代码,下面是所使用的XML代码:

[html]view plain

<?xmlversion="1.0"encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<!--第一部分-->

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN"/>

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="OneButton"/>

<!--第二部分-->

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN"/>

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="SecondButton"/>

<!--最后的按钮-->

<Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="AnotherButton"/>

</LinearLayout>

这段代码理解起来一点难度没有,就是几个TextView和几个Button,下面我们用include把这段代码给分割成几个文件,并完成相同的效果;

二、使用Include时

1、先将上面代码标记有“第一部分”的,代码段分离成一个文件(sublayout1.xml);

[html]view plain

<?xmlversion="1.0"encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#505050"

android:orientation="vertical">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN"/>

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="OneButton"/>

</LinearLayout>

2、再将标记有“第二部分”的代码段,分离成第二个文件(sublayout2.xml):

[html]view plain

<?xmlversion="1.0"encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN"/>

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="SecondButton"/>

</LinearLayout>

3、主文件中使用include,将上面两个文件包含进去(activity_main.xml);

[html]view plain

<?xmlversion="1.0"encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<include

android:id="@+id/main1"

layout="@layout/sublayout1"/>

<include

android:id="@+id/main2"

layout="@layout/sublayout2"/>

<Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="AnotherButton"/>

</LinearLayout>

这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。

4. android tabbar 怎么添加布局进去

如下代码:
LinearLayout layout = new LinearLayout(this);
TextView tx = new TextView(this);
tx.setText('我是动态添加的');
layout.addView(tx);
setContentView(layout);

这就动态添加了一个线性布局,并且在布局里面加了一个textview

阅读全文

与android代码添加布局相关的资料

热点内容
文件名修改后快捷方式还可以用吗 浏览:387
亲密爱人qq相册名字 浏览:884
怎么修改手机原始密码 浏览:230
榆次网站优化怎么样 浏览:537
vb窗体文件如何打开 浏览:810
百度找图怎么跳转网站 浏览:284
安卓44如何隐藏文件夹 浏览:92
sr100怎么编程序 浏览:795
苹果手机怎样保存信息 浏览:379
安卓patch是什么文件 浏览:819
2003word审阅 浏览:387
手机时间查找新文件 浏览:695
数据库管理员怎么入行 浏览:852
安卓nba2k17修改能力 浏览:868
win10设置nsf文件共享 浏览:445
同一个网站怎么发文章 浏览:725
苹果翻盖手机多少钱 浏览:815
flash80金鹰教程 浏览:374
怎么把美国的app账号换成中国的 浏览:375
贷款60秒app 浏览:408

友情链接