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>

源码地址:http://download.csdn.net/detail/double2hao/9428144

时间: 2024-08-03 03:00:26

android 用java动态设置布局(增添删除修改布局)的相关文章

Android 手机卫士--实现设置界面的一个条目布局结构

本文地址:http://www.cnblogs.com/wuyudong/p/5908986.html,转载请注明源地址. 本文以及后续文章,将一步步完善功能列表: 要点击九宫格中的条目,需要注册点击事件 // 注册九宫格单个条目的点击事件 gv_home.setOnItemClickListener(new OnItemClickListener() { // 点中列表条目索引 position @Override public void onItemClick(AdapterView<?>

android 静态和动态设置 Receiver的 android:enabled值

0x 01 前提约束: 0x001 静态检查:指用action限定Intent,并使用包管理器的queryBroadCastReceivers方法,在flags字段置为0时查找ResolveInfo,检查结果是指它有没有找到组件. 0x002          动态检查:指在指定 包名&类全路径名构成的ComponentName后,调用包管理器的getComponentEnabledSetting方法,得到它的状态值,注意它是一个Int类型,可能取值及意义如下所列. 0x003        

android 用java代码设置布局、视图View的宽度/高度或自适应

在achat项目中,对话内容的长宽设置为自适应,但是如果文本内容太多,则宽度差不多布满,若自己说的和对方说的都很多内容,则满屏都是文字,则不容易分辨出是来自别人说的还是自己说的.那么需要对本身具有自适应的布局再来个宽度限制. 首先,在xml里面定义layout的layout_width/layout_height都要设置为wrap_content,然后在getView方法里面,一开始就让这个布局wrap_content(为什么xml里面设置了wrap_content,这里还要再设置一遍呢?因为布

android自定义控件,动态设置Button的样式

今天来看一个通过重写Button来动态实现一些效果,如圆角矩形.圆形.按下改变字体,改变背景色,改变背景图等 在此说明一下,这种实现方式绝对不是唯一的,而且通过xml文件即可简单实现,这样做只是为了将控件的样式完全由代码实现,更方便打包应用于其他项目 下面来看几张效果图:   图1 初始状态                                            图2 按下第一行的TEXT0    图3 按下第二行的TEXT1                         图4

Android代码中动态设置图片的大小(自动缩放),位置

项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

Java动态 遍历List 时删除List特征元素 异常问题 及解决方式总结

首先,这是一个极其简单的问题,大牛可忽略,新手可能会遇到,Java中遍历某个List 时删除该List元素 会抛出异常. 这一个简单的问题再高手严重不值一提,但新手可能会比较困惑,用哪种方式可以安全有效的实现遍历list删除某些特征元素? 方式: 方法1.普通for循环 遍历List,删List除自身 特征条目: 方法2.高级for循环 遍历List,删除List自身 特定条目: 方法3.引入参考List,for循环遍历删除原List 特定条目: 方法4.利用iterator 遍历删除List特

Android开发之动态设置字体的样式和粗细

字体设置通常有两种形式: 1:在xml中直接设置 android:textStyle="bold" android:typeface="sans" 2:用java代码的形式设置 在Android SDK中使用Typeface类来定义字体,可以通过常用字体类型名称进行设置,如设置斜体: TextView quehuo = (TextView)findViewById(R.id.quehuo);//为TextView显示的字体设置样式 quehuo.setTypefac

【Android基础】动态设置颜色值的方法

在Android里我们通常都是在Xml里设置字体或者图片等等的颜色值,但是有些时候我们要用到动态的设置颜色值(说白了就是在代码里设置颜色值),这个时候可能有些朋友不太清除这方面,所以我总结了以下两个方式(暂时): 1.通过Color这个API获取RGB颜色值,例: tvContent.setTextColor(Color.parseColor("#969696")); 2.通过getResources函数获取到Resources,接着用getColor方法获取对应的color文件下的颜

[Android] 代码中动态设置shape

TextView textView = new TextView(this); GradientDrawable drawable = new GradientDrawable(); drawable.setCornerRadius(5); drawable.setStroke(1, Color.parseColor("#cccccc")); drawable.setColor(Color.parseColor("#eeeeee")); textView.setBa