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

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

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

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

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

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

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, 0, 0, 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, 0, 0, 10);//设置margin,此处单位为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:

<?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:

<?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> 

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

(0)

相关推荐

  • Java弹簧布局管理器使用方法详解

    由 SpringLayout 类实现的布局管理器称为弹簧布局管理器.利用该布局管理器管理组件,当改变窗体的大小时,能够在不改变组件间相对位置的前提下自动调整组件大小,使组件依旧布满整个窗体,从而保证了窗体的整体效果.下面,通过一个实例来看看弹簧布局管理器的使用方法和效果. 弹簧布局管理器以容器和组件的边缘为操作对象,通过为组件和容器边缘以及组件和组件边缘建立约束,实现对组件布局的管理.通过方法 putConstraint(String e1,Conponet c1,int pad,String

  • java Swing布局管理之BoxLayout布局

    本文为大家解析java Swing布局管理中的BoxLayout布局,供大家参考,具体内容如下 BoxLayout:可以指定在容器中是否对控件进行水平或者垂直放置,比 FlowLayout 要更为灵活 BoxLayout与其他布局管理器稍有不同,必须向其构造函数中传递容器实例的引用,由该容器使用BoxLayout.另外必须指定BoxLayout中组件的布局方式:垂直排列(按列)或水平排列(按行).用水平组件和垂直组件的不同组合嵌套多面板的作用类似于 GridBagLayout,但没那么复杂. 1

  • Java 最重要布局管理器GridBagLayout的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同便可以将组件垂直.水平或沿它们的基线对齐. 每个 GridBagLayout 对象维持一个动态的矩形单元网格,每个组件占用一个或多个这样的单元,该单元被称为显示区域. 下面就通过一个记事本案例去说明GridBagLayout的使用方法. 分析: 带有箭头的说明可以拉伸的. 4占用4个格

  • java图形界面之布局设计

    在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计.java.awt中定义了多种布局类,每种布局类对应一种布局的策略.常用的有以下布局类: •FlowLayout,依次放置组件. •BoarderLayout,将组件放置在边界上. •CardLayout,将组件像扑克牌一样叠放,而每次只能显示其中一个组件. •GridLayout,将显示区域按行.列划分成一个个相等的格子,组件依次放入这些格子中. •GridBagLayout,将显示区域划分成许多矩形小单元

  • 第一次编写Java流布局图形界面

    本文实例为大家分享了Java流布局图形界面编写代码,供大家参考,具体内容如下 package jisuanqi; import java.awt.*; public class MyFrame extends Frame{ //继承Frame类 public MyFrame() { super("第一个图形界面"); //设置框架窗口标题 this.setSize(200, 130); //设置组件尺寸(宽,高) this.setLocation(300, 240); //设置组件的显

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

    XML对开发者来说十分的方便,不仅使用起来简单,而且能够及时调试,修改界面之后马上能看到效果. Java设置布局不具有这个优势.但是java却可以动态对布局进行操作,这是xml所做不到的.笔者认为,新手索要掌握的java动态设置布局主要有两点,一方面是对布局的属性进行修改,另一方面是增添和删除控件. 首先说一下动态设置布局在项目中的应用,拿高德地图举个例子,如下图: 我们可以看到,高德地图的默认界面与点击地图之后的界面是不一样的,上面同样的控件在layout中的位置也不一样,这个用xml便是难以

  • java怎么创建目录(删除/修改/复制目录及文件)代码实例

    复制代码 代码如下: import java.io.*; public class FileOperate {   public FileOperate() {   } /**    * 新建目录    * @param folderPath String 如 c:/fqf    * @return boolean    */   public void newFolder(String folderPath) {     try {       String filePath = folder

  • Android GridView扩展仿微信微博发图动态添加删除图片功能

    在平时的开发中,我们会看到不管是微信发朋友圈照片还是微博发布新鲜事,添加图片的时候都是选完后面还有个+号再去选择图片,这样的话比较方便用户去添加图片,有的右上角还有个-号方便用户去删除图片,而一般用户选择的图片多少都是不定的,我们只限制最大张数,我们用gridview去实现,代码可能比较简单,高手请略过. 0.效果图 1.准备资源图片 添加图片的+号图片 删除图片的图片 2.可设置限制用户选择最大张数 /** * 可以动态设置最多上传几张,之后就不显示+号了,用户也无法上传了 * 默认9张 */

  • Android中使用TagFlowLayout制作动态添加删除标签

    效果图 简单的效果图(使用开源库)[FlowLayout](" https://github.com/hongyangAndroid/FlowLayout ") 步骤 导包 compile 'com.zhy:flowlayout-lib:1.0.3' <com.zhy.view.flowlayout.TagFlowLayout android:id="@+id/id_flowlayout" zhy:max_select="-1" andro

  • javascript动态添加、修改、删除对象的属性与方法详解

    现在介绍如何为一个对象添加.修改或者删除属性和方法.在其他语言中,对象一旦生成,就不可更改了,要为一个对象添加修改成员必须要在对应的类中修改,并重新实例化,而且程序必须经过重新编译.JavaScript 中却非如此,它提供了灵活的机制来修改对象的行为,可以动态添加.修改.删除属性和方法.例如首先使用类Object来创建一个空对象user:var user=new Object(); 1.添加属性这时user 对象没有任何属性和方法,显然没有任何用途.但可以为它动态的添加属性和方法,例如:user

  • JavaScript数组,JSON对象实现动态添加、修改、删除功能示例

    本文实例讲述了JavaScript数组,JSON对象实现动态添加.修改.删除功能.分享给大家供大家参考,具体如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>javascript里面的数组,json对象,动态添加,修改,删除示例</t

  • Android WebView通过动态的修改js去拦截post请求参数实例

    需求背景: 需要在用户点击提交按钮的时候拦截用户提交的数据. 遇到的问题: 1.页面不是自家前端做的,不能修改网页中的代码 2.要拦截的请求不是get请求,而是一个post请求 (难点在于:如果拦截的请求是get请求的话,我只需要拿到url,将后面拼接的参数键值对取出来就好了,但是post请求的参数键值对我们是看不到的...) 解决重点: 重写webViewClient的shouldInterceptRequest这个方法 1.这个方法是API21以后才出现的,还有一个过时的方法也要重写,不要忘

  • 揭秘在ListView等AdapterView上动态添加删除项的陷阱

    如何避开在ListView等AdapterView上动态添加删除项的陷阱,下面就为大家分享,具体内容如下 首先,定义如下array资源,作为列表的加载内容: <resources> <string name="app_name">MyListView</string> <string-array name="language"> <item>Java</item> <item>C&l

  • Android编程实现动态更新ListView的方法

    本文实例讲述了Android编程实现动态更新ListView的方法.分享给大家供大家参考,具体如下: 有时候我们需要修改已经生成的列表,添加或者修改数据,notifyDataSetChanged()可以在修改适配器绑定的数组后,不用重新刷新Activity,通知Activity更新ListView.今天的例子就是通过Handler AsyncTask两种方式来动态更新ListView.从今天起,每次学习的源代码都会打包上传,方便各位同学学习,注册帐号即可下载. 布局main.xml: <?xml

  • Android Fragment(动态,静态)碎片详解及总结

    Android Fragment(动态,静态)碎片详解 一.Fragment的相关概念(一)Fragment的基础知识 Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户界面.使用多个Fragment可以在一个单独的Activity中建 立多个UI面板,也可以在多个Activity中使用Fragment. Fragment拥有自己的生命 周期和接收.处理用户的事件,这样就不必在Activity写一

随机推荐