资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用java代码写布局 java的布局

android studio 不用xml直接使用java代码进行布局.好的可以加分

// 使用代码布局

网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站制作、成都做网站收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了十年的创新互联网站建设公司。

// root layout

LinearLayout root = new LinearLayout(this);

root.setOrientation(LinearLayout.VERTICAL);

// top layout

LinearLayout topLayout = new LinearLayout(this);

LinearLayout.LayoutParams topParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);

topParams.weight = 1;

topLayout.setLayoutParams(topParams);

// left child

View leftView = new View(this);

leftView.setBackgroundColor(Color.RED);

LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);

leftParams.weight = 1;

leftView.setLayoutParams(leftParams);

topLayout.addView(leftView);

// right Layout

LinearLayout rightLayout = new LinearLayout(this);

rightLayout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams rightParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);

rightParams.weight = 1;

rightLayout.setLayoutParams(rightParams);

// right top child

View rightTopView = new View(this);

rightTopView.setBackgroundColor(Color.YELLOW);

LinearLayout.LayoutParams rightTopParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);

rightTopParams.weight = 1;

rightTopView.setLayoutParams(rightTopParams);

// add right top view

rightLayout.addView(rightTopView);

// right bottom child

View rightBottomView = new View(this);

rightBottomView.setBackgroundColor(Color.BLACK);

LinearLayout.LayoutParams rightBottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);

rightBottomParams.weight = 1;

rightBottomView.setLayoutParams(rightBottomParams);

// add right bottom view

rightLayout.addView(rightBottomView);

// add right layout

topLayout.addView(rightLayout);

// add top layout

root.addView(topLayout);

// bottom layout

LinearLayout bottomLayout = new LinearLayout(this);

bottomLayout.setBackgroundColor(Color.BLUE);

LinearLayout.LayoutParams bottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);

bottomParams.weight = 1;

bottomLayout.setLayoutParams(bottomParams);

root.addView(bottomLayout);

// setContentView(R.layout.activity_main);

setContentView(root);

怎么用java代码写一个线性布局;布局里面有两个按钮是水平的

android 使两个按钮水平排列的方法是使用lineLayout线性布局,如下代码:

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

LinearLayout xmlns:android=""

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:background="@color/background"

android:orientation="vertical" 

View

android:layout_width="wrap_content"

android:layout_height="1.2px"

android:layout_marginBottom="7dp"

android:background="@color/white" /

LinearLayout

android:layout_width="fill_parent"

android:layout_height="79dp"

android:layout_weight="2"

android:orientation="horizontal"

android:layout_margin="10dp" 

Button

android:id="@+id/bt1"

android:layout_width="fill_parent"

android:layout_height="26dp"

android:background="@drawable/shape"

android:layout_weight="1"

android:text="确认对冲"

android:textColor="@color/white"

android:textSize="15dp" /

Button

android:layout_width="fill_parent"

android:layout_height="26dp"

android:background="@drawable/shapeyuanjiao"

android:layout_weight="1"

android:text="取消"

android:textColor="@color/white"

android:textSize="15dp" /

/LinearLayout

/LinearLayout

运行结果如下:

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

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

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

一、未使用Include时

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

[html] view plaincopy

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

LinearLayout xmlns: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=" One Button " /

!-- 第二部分 --

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=" Second Button " /

!-- 最后的按钮 --

Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" Another Button " /

/LinearLayout

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

二、使用Include时

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

[html] view plaincopy

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

LinearLayout xmlns: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=" One Button " /

/LinearLayout

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

[html] view plaincopy

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

LinearLayout xmlns: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=" Second Button " /

/LinearLayout

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

[html] view plaincopy

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

LinearLayout xmlns: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=" Another Button " /

/LinearLayout

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

Android Studio如果在java中编写布局,代码放在哪个文件中?什么位置?

1.使用代码编写一个底部选项卡的布局

2.整个页面的容器布局(包含Fargment,分割线,选项卡)

private void initView(Context context) {

setBackgroundColor(0xfff6f6f6);

FrameLayout frameLayout=new FrameLayout(context);//选项界面容器

frameLayout.setId(FL_ID);

View lineView=new View(context);//分割线

lineView.setId(LINE_ID);

RelativeLayout.LayoutParams rlParams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

rlParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

rlParams.addRule(RelativeLayout.ABOVE , LINE_ID);

lineView.setBackgroundColor(lineColor);

RelativeLayout.LayoutParams rlParams2=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, DensityUtils.dip2px(context, 1));

rlParams2.addRule(RelativeLayout.ABOVE , TAB_ID);

addView(frameLayout, rlParams);//选项界面容器

addView(lineView,rlParams2);//分割线

//选项卡容器

linearLayout=new LinearLayout(context);

linearLayout.setBackgroundColor(tabBgColor);

linearLayout.setOrientation(LinearLayout.HORIZONTAL);

linearLayout.setGravity(Gravity.CENTER_VERTICAL);

linearLayout.setId(TAB_ID);

tabNum=tabIcos.length;

for (int i = 0; i tabNum; i++) {

View view = createIndicator(tabIcos[i], tabtxts[i], tabItemTvColor, "itemTag"+i, "icoTag" + i, "txtTag" + i);

view.setOnClickListener(OnClick);

if(i== nowTabIndex){//初始化选项卡

changeTab(view, i);

}

linearLay

android 在activity里用java代码写Xml布局文件

你是想在activity的代码里写linearlayout么?

1、你可以在代码里面创建一个LinearLayout (比如 lineLayout1 ),然后针对这个变量进行设置

2、然后你需要通过findViewById()的方法,去查找xml定义好的那个ScrollView,把他放入一个变量中,如view1,当然前提是你要再xml里面给这个ScrollView起一个名字

3、调用view1.add(lineLayout1)方法把lineLayout1加进去

当然这是一个大方向,具体的代码细节你要再研究一下


当前文章:用java代码写布局 java的布局
浏览路径:http://cdkjz.cn/article/dodeeps.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220