android用java動(dòng)態(tài)增添刪除修改布局
XML對(duì)開發(fā)者來說十分的方便,不僅使用起來簡單,而且能夠及時(shí)調(diào)試,修改界面之后馬上能看到效果。
Java設(shè)置布局不具有這個(gè)優(yōu)勢(shì)。但是java卻可以動(dòng)態(tài)對(duì)布局進(jìn)行操作,這是xml所做不到的。筆者認(rèn)為,新手索要掌握的java動(dòng)態(tài)設(shè)置布局主要有兩點(diǎn),一方面是對(duì)布局的屬性進(jìn)行修改,另一方面是增添和刪除控件。
首先說一下動(dòng)態(tài)設(shè)置布局在項(xiàng)目中的應(yīng)用,拿高德地圖舉個(gè)例子,如下圖:

我們可以看到,高德地圖的默認(rèn)界面與點(diǎn)擊地圖之后的界面是不一樣的,上面同樣的控件在layout中的位置也不一樣,這個(gè)用xml便是難以實(shí)現(xiàn)的了,于是java動(dòng)態(tài)設(shè)置布局便有了其重要性。
接下來看一下分享的demo效果:

代碼其實(shí)比較容易理解,具體的解釋已經(jīng)注釋在代碼中了,讀者可以自己寫了理解一下。
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(這個(gè)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設(shè)置不可見
//初始化寬高屬性
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//設(shè)置置底
lp1.setMargins(10, 0, 0, 10);//設(shè)置margin,此處單位為px
RL_RadioGroup.setLayoutParams(lp1);//動(dòng)態(tài)改變布局
break;
case R.id.button2:
RL_InfoTip.setVisibility(View.VISIBLE);//底部tip設(shè)置可見
//初始化寬高屬性
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.setMargins(10, 0, 0, 10);//設(shè)置margin,此處單位為px
lp2.addRule(RelativeLayout.ABOVE, R.id.info_tip);//設(shè)置above,讓控件于R.id.info_tip之上
RL_RadioGroup.setLayoutParams(lp2);//動(dòng)態(tài)改變布局
break;
case R.id.button3:
//初始化寬高屬性,此處單位為px
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(200, 200);
lp3.addRule(RelativeLayout.BELOW, R.id.button4);//設(shè)置below,讓控件于R.id.button4之下
RL_main.addView(LL_test, lp3);//動(dòng)態(tài)改變布局
LL_test.setVisibility(View.VISIBLE);//此處需要設(shè)置布局顯示,否則會(huì)不顯示
break;
case R.id.button4:
RL_main.removeView(LL_test);//動(dòng)態(tài)改變布局
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="精準(zhǔn)"
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="受災(zāi)地點(diǎn)"
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="受災(zāi)距離"/>
<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="受災(zāi)地址"/>
<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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android動(dòng)態(tài)布局小結(jié)
- android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
- 基于Android在布局中動(dòng)態(tài)添加view的兩種方法(總結(jié))
- Android 動(dòng)態(tài)改變布局實(shí)例詳解
- Android編程動(dòng)態(tài)加載布局實(shí)例詳解【附demo源碼】
- Android動(dòng)態(tài)加載布局
- Android動(dòng)態(tài)添加設(shè)置布局與控件的方法
- android動(dòng)態(tài)加載布局文件示例
- Android動(dòng)態(tài)布局使用詳解
相關(guān)文章
基于Android實(shí)現(xiàn)滾動(dòng)頭部懸停效果
這篇文章主要為大家詳細(xì)介紹了如何在?Android?中實(shí)現(xiàn)這種滾動(dòng)頭部懸停效果,并提供完整源碼,方便學(xué)習(xí)和實(shí)際應(yīng)用,有需要的小伙伴可以了解一下2025-04-04
Android應(yīng)用獲取設(shè)備序列號(hào)的方法
本篇文章主要介紹了Android應(yīng)用獲取設(shè)備序列號(hào)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Android藍(lán)牙的開啟和搜索設(shè)備功能開發(fā)實(shí)例
這篇文章主要介紹了Android藍(lán)牙服務(wù)啟動(dòng)搜索流程,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-04-04
解析Android中Animation動(dòng)畫的編寫要點(diǎn)
這篇文章主要介紹了Android中Animation動(dòng)畫的編寫要點(diǎn),講解了Animation的幾個(gè)常用標(biāo)簽的用法,需要的朋友可以參考下2016-04-04
基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05
Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法,簡單分析了Android中Activity數(shù)據(jù)共享與訪問的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android布局中g(shù)ravity與layout_gravity屬性說明
這篇文章主要介紹了Android布局中g(shù)ravity與layout_gravity屬性說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

