zoukankan      html  css  js  c++  java
  • 安卓布局优化--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("加载更多");
    				}
    
    			}
    		});
    	}
    
    }
    

    效果图如下:



  • 相关阅读:
    Head First 设计模式 --11 代理模式
    spring-boot启动信息中non-fatal error
    在eclipse中用gradle搭建MapReduce项目
    ubuntu14.04 Hadoop单机开发环境搭建MapReduce项目
    Android log日志
    Android studio编译之后显示中文乱码的问题解决办法
    Android官方文档
    Android应用程序中应用图标和名字的设置
    转:java提取图片中的像素
    Android手机开机自动启动
  • 原文地址:https://www.cnblogs.com/emoji/p/4436786.html
Copyright © 2011-2022 走看看