安卓布局优化--ViewStub使用

在开发中很多时候回遇到这种情况,我们在一般情况下不需要在界面上显示那么多控件,但是这些控件在特殊情况下又是需要被显示的,此时ViewStub就派上用场了,话不多说,直接代码讲解



首先我们定义一个布局(就是那个时而隐藏时而显示的布局啦生气)  viewstub.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText
        android:id="@+id/text1"
        android:hint="@string/moremsg" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <EditText
        android:id="@+id/text2"
        android:hint="@string/moremsg" 
        android:layout_width="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/text3"
        android:hint="@string/moremsg" 
        android:layout_width="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"/>
    
</LinearLayout>

效果很简单,就是三个垂直排列的输入框。


接下来开始定义主布局(注意ViewStub只是一个控件,所以他的用法和Button这些没什么不同)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linear"
    tools:context="com.example.viewstub.MainActivity" >

    <EditText 
        android:hint="输入姓名"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <EditText 
        android:hint="在此输入电话"
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    
    <Button 
        android:id="@+id/more"
        android:textSize="25sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="加载更多"/>
    
    <ViewStub 
        android:id="@+id/viewstub"
        android:layout_width="fill_parent"
        android:layout="@layout/viewstub"
        android:layout_height="wrap_content"/>

</LinearLayout>

然后开始在主函数调用:


package com.example.viewstub;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

	private ViewStub viewstub;
	private EditText text1, text2, text3;
	private Button more;
	private int flag; // 标记是否已经展开隐藏项 1代表是
	private View layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewstub = (ViewStub) findViewById(R.id.viewstub);
		this.more = (Button) findViewById(R.id.more);
		flag = 0;
		this.more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (flag == 0) {
					// viewstub.setVisibility(View.VISIBLE);
					if (viewstub != null) {
						View view = viewstub.inflate();
						text1 = (EditText) view.findViewById(R.id.text1);
						text2 = (EditText) view.findViewById(R.id.text2);
						text3 = (EditText) view.findViewById(R.id.text3);
						view = null;
					}
					flag = 1;
					more.setText("收起更多");
				} else {
					flag = 0;
					viewstub.setVisibility(View.INVISIBLE);
					more.setText("加载更多");
				}

			}
		});
	}

}

效果图如下:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值