android用java动态增添删除修改布局

android用java动态增添删除修改布局,第1张

概述XML对开发者来说十分的方便,不仅使用起来简单,而且能够及时调试,修改界面之后马上能看到效果。

XML对开发者来说十分的方便,不仅使用起来简单,而且能够及时调试,修改界面之后马上能看到效果。
Java设置布局不具有这个优势。但是java却可以动态对布局进行 *** 作,这是xml所做不到的。笔者认为,新手索要掌握的java动态设置布局主要有两点,一方面是对布局的属性进行修改,另一方面是增添删除控件。@H_301_2@

首先说一下动态设置布局在项目中的应用,拿高德地图举个例子,如下图:
@H_301_2@

 

@H_301_2@@H_301_2@

我们可以看到,高德地图的默认界面与点击地图之后的界面是不一样的,上面同样的控件在layout中的位置也不一样,这个用xml便是难以实现的了,于是java动态设置布局便有了其重要性。@H_301_2@

接下来看一下分享的demo效果:
@H_301_2@

@H_301_2@@H_301_2@

代码其实比较容易理解,具体的解释已经注释在代码中了,读者可以自己写了理解一下。
MainActivity:
@H_301_2@

package com.example.activeuitest;  import androID.support.v7.app.AppCompatActivity; import androID.os.Bundle; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.button; import androID.Widget.linearLayout; import androID.Widget.RadioGroup; import androID.Widget.relativeLayout;  public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{    private button BT_Gone;//让布局隐藏   private button BT_Visiable;//让布局显示   private button BT_Add;//增添布局   private button BT_Delete;//删除布局    private relativeLayout RL_main;   private RadioGroup RL_RadioGroup;   private relativeLayout RL_InfoTip;   private linearLayout LL_test;    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);      init();//初始化   }    private voID init() {     BT_Gone= (button) findVIEwByID(R.ID.button1);     BT_Visiable= (button) findVIEwByID(R.ID.button2);     BT_Add= (button) findVIEwByID(R.ID.button3);     BT_Delete= (button) findVIEwByID(R.ID.button4);      RL_main=(relativeLayout)findVIEwByID(R.ID.main_layout);     RL_RadioGroup=(RadioGroup)findVIEwByID(R.ID.radio_group);     RL_InfoTip=(relativeLayout)findVIEwByID(R.ID.info_tip);      //此处要获取其他xml的控件需要先引入改layout的vIEw(这个linearlayout用于演示添加和删除)     VIEw vIEw= LayoutInflater.from(this).inflate(R.layout.test_linear_layout,null,false );     LL_test=(linearLayout)vIEw.findVIEwByID(R.ID.test_layout);      BT_Gone.setonClickListener(this);     BT_Visiable.setonClickListener(this);     BT_Add.setonClickListener(this);     BT_Delete.setonClickListener(this);   }    @OverrIDe   public voID onClick(VIEw v) {     switch(v.getID()){       case R.ID.button1:         RL_InfoTip.setVisibility(VIEw.GONE);//底部tip设置不可见         //初始化宽高属性         relativeLayout.LayoutParams lp1 = new relativeLayout.LayoutParams(             VIEwGroup.LayoutParams.WRAP_CONTENT,VIEwGroup.LayoutParams.WRAP_CONTENT);         lp1.addRule(relativeLayout.AliGN_PARENT_BottOM);//设置置底         lp1.setmargins(10,10);//设置margin,此处单位为px         RL_RadioGroup.setLayoutParams(lp1);//动态改变布局         break;       case R.ID.button2:         RL_InfoTip.setVisibility(VIEw.VISIBLE);//底部tip设置可见         //初始化宽高属性         relativeLayout.LayoutParams lp2 = new relativeLayout.LayoutParams(             VIEwGroup.LayoutParams.WRAP_CONTENT,VIEwGroup.LayoutParams.WRAP_CONTENT);         lp2.setmargins(10,此处单位为px         lp2.addRule(relativeLayout.ABOVE,R.ID.info_tip);//设置above,让控件于R.ID.info_tip之上         RL_RadioGroup.setLayoutParams(lp2);//动态改变布局         break;       case R.ID.button3:         //初始化宽高属性,此处单位为px         relativeLayout.LayoutParams lp3 = new relativeLayout.LayoutParams(200,200);         lp3.addRule(relativeLayout.BELOW,R.ID.button4);//设置below,让控件于R.ID.button4之下         RL_main.addVIEw(LL_test,lp3);//动态改变布局         LL_test.setVisibility(VIEw.VISIBLE);//此处需要设置布局显示,否则会不显示         break;       case R.ID.button4:         RL_main.removeVIEw(LL_test);//动态改变布局         break;     }   } } 

activity_main:
@H_301_2@

<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:ID="@+ID/main_layout"    >     <button     androID:ID="@+ID/button1"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="隐藏"/>   <button     androID:ID="@+ID/button2"     androID:layout_below="@+ID/button1"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="显示"/>   <button     androID:ID="@+ID/button3"     androID:layout_below="@+ID/button2"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="添加布局"/>   <button     androID:ID="@+ID/button4"     androID:layout_below="@+ID/button3"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:text="删除布局"/>   <RadioGroup     androID:ID="@+ID/radio_group"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:padding="5dp"     androID:layout_marginleft="10px"     androID:layout_marginBottom="10px"     androID:orIEntation="horizontal"     androID:layout_above="@+ID/info_tip"     androID:background="@androID:color/darker_gray"     >      <TextVIEw       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="精确度:"/>      <Radiobutton       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:checked="true"       androID:text="普通"       androID:textcolor="@androID:color/black" />      <Radiobutton       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="精准"       androID:textcolor="@androID:color/black" />   </RadioGroup>    <relativeLayout     androID:ID="@+ID/info_tip"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_alignParentBottom="true"     androID:paddingleft="10dp"     androID:paddingRight="10dp"     androID:paddingtop="20dp"     androID:background="@androID:color/darker_gray"     >      <TextVIEw       androID:ID="@+ID/info_tip_name"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="受灾地点"       androID:textcolor="@androID:color/black"       androID:textSize="20dp"/>     <TextVIEw       androID:ID="@+ID/info_tip_distance"       androID:layout_below="@+ID/info_tip_name"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="受灾距离"/>     <TextVIEw       androID:ID="@+ID/info_tip_address"       androID:layout_toRightOf="@+ID/info_tip_distance"       androID:layout_below="@+ID/info_tip_name"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:layout_marginleft="10dp"       androID:text="受灾地址"/>      <button       androID:layout_alignParentRight="true"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="详情"/>      <linearLayout       androID:layout_below="@+ID/info_tip_address"       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:layout_margintop="10dp"       androID:orIEntation="horizontal">       <button         androID:layout_wIDth="0dp"         androID:layout_weight="1"         androID:layout_height="wrap_content"         androID:text="驾车"/>       <button         androID:layout_wIDth="0dp"         androID:layout_weight="1"         androID:layout_height="wrap_content"         androID:text="公交"/>       <button         androID:layout_wIDth="0dp"         androID:layout_weight="1"         androID:layout_height="wrap_content"         androID:text="步行"/>     </linearLayout>    </relativeLayout> </relativeLayout> 

test_linear_layout:
@H_301_2@

<?xml version="1.0" enCoding="utf-8"?> <linearLayout   xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="200dp"   androID:layout_height="200dp"   androID:background="@androID:color/holo_blue_bright"   androID:ID="@+ID/test_layout"   androID:orIEntation="horizontal"   >  </linearLayout> 

以上就是本文的全部内容,希望对大家的学习有所帮助。@H_301_2@ 总结

以上是内存溢出为你收集整理的android用java动态增添删除修改布局全部内容,希望文章能够帮你解决android用java动态增添删除修改布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149504.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存