ViewStub用法
来源:互联网 发布:淘宝联名信用卡怎么样 编辑:程序博客网 时间:2024/06/03 13:20
在开发应用程序,会在运行时动态根据条件来决定显示哪个View或某个布局。那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE。这样的做法的优点是逻辑简单而且控制起来比较灵活。但是它的缺点就是,耗费资源。
login_register_btn_layout.xml
merage_account_info_layout.xml
MainActivity
推荐的做法是使用Android.view.ViewStub,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化。
使用的时候的注意事项:
1. 某些布局属性要加在ViewStub而不是实际的布局上面,才会起作用,比如上面用的android:layout_margin*系列属性,如果加在TextView上面,则不会起作用,需要放在它的ViewStub上面才会起作用。而ViewStub的属性在inflate()后会都传给相应的布局。apply_indentify_layout1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#333333" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/iv_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ViewStub android:id="@+id/loginRegisterViewStub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="17dp" android:layout="@layout/login_register_btn_layout" /> <ViewStub android:id="@+id/accountInfoViewStub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="17dp" android:layout="@layout/merage_account_info_layout" /> </LinearLayout></LinearLayout>
login_register_btn_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <Button android:id="@+id/login" android:layout_width="100dp" android:layout_height="30dp" android:text="登录" android:textColor="#ffffff" android:textSize="16sp" android:gravity="center"/></LinearLayout>
merage_account_info_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="13dp" android:layout_gravity="center_horizontal" android:text="Dany" android:textColor="#ffffff" android:textSize="16sp" /> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="11dp" android:layout_gravity="center_horizontal" android:text="132-8165-0600" android:textColor="#ffffff" android:textSize="14sp" /></LinearLayout>
MainActivity
package com.example.viewstud;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.ViewStub;import android.widget.TextView;public class MainActivity extends Activity {View accountInfoView, loginRegisterView;private SharedPreferences sharedPreferences;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.apply_indentify_layout1);sharedPreferences = getSharedPreferences("safly", Context.MODE_PRIVATE);// 默认falseboolean safly = sharedPreferences.getBoolean("safly", false);if (safly) {if (loginRegisterView != null)loginRegisterView.setVisibility(View.GONE);if (accountInfoView == null) {accountInfoView = ((ViewStub) findViewById(R.id.accountInfoViewStub)).inflate();} else {accountInfoView.setVisibility(View.VISIBLE);}} else {if (accountInfoView != null)accountInfoView.setVisibility(View.GONE);if (loginRegisterView == null) {loginRegisterView = ((ViewStub) findViewById(R.id.loginRegisterViewStub)).inflate();} else {loginRegisterView.setVisibility(View.VISIBLE);}}findViewById(R.id.iv_photo).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {sharedPreferences.edit().putBoolean("safly", true).commit();sharedPreferences.edit().clear();}});}}
0 0
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法
- ViewStub用法介绍
- Android ViewStub的用法
- Android ViewStub的用法
- viewstub用法(android)
- ViewStub基本用法
- ViewStub 用法详解
- ViewStub基本用法(延迟初始化)
- Android之viewstub用法详解
- velocity freemarker比较
- Python新手学习基础之数据类型——变量
- 台湾大学林轩田教授机器学习基石课程理解及python实现----PLA
- How to implement check box in listView Android to delete listItems
- nodejs入门之使用nodejs展示一个网页
- ViewStub用法
- POJ——2449Remmarguts' Date(A*+SPFA)
- 自定义view详解,手把手带你画一个漂亮蜂窝view Android自定义view
- C++Primer(5th)第三章学习总结-vector篇
- 支付宝支付
- 取石子问题
- target XXX do ->end 常见问题
- 浅谈Java设计模式(十九)备忘录模式(Memento)
- libcurl CURLOPT_WRITEFUNCTION注意事项