Android ViewStub含代碼

    Android ViewStub

    ViewStub 是一個零尺寸不可見的視圖,用于在運行時加載“布局資源”。愛掏網 - it200.comViewStub是一個零尺寸的視圖,所以在布局目錄中看不到任何內容。愛掏網 - it200.com

    為了使父資源可見, 調用inflate() 方法。愛掏網 - it200.com為了使ViewStub可見或不可見, 調用setVisibility(int) 方法。愛掏網 - it200.com使用View.VISIBLE常量使ViewStub可見,使用View.GONE常量使其不可見。愛掏網 - it200.com

    ViewStub示例

    讓我們創建一個ViewStub視圖的示例,該視圖顯示并隱藏在另一個布局(my_layout.xml)文件中創建的ImageView(image)。愛掏網 - it200.com

    文件:activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.test.viewstubexample.MainActivity">
    
        <ViewStub
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/viewStub"
            android:layout_marginLeft="120dp"
            android:layout="@layout/my_layout"
            android:layout_alignParentTop="true" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show"
            android:id="@+id/show"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="65dp"
            android:layout_marginStart="65dp" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hide"
            android:id="@+id/hide"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/show"
            android:layout_toEndOf="@+id/show"
            android:layout_marginLeft="51dp"
            android:layout_marginStart="51dp" />
    
    </RelativeLayout>
    

    文件:my_layout.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">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:background="@drawable/image"
            />
    
    </LinearLayout>
    

    文件: MainActivity.java

    package com.example.test.viewstubexample;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewStub;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        ViewStub viewStub;
        Button show,hide;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            show=(Button)findViewById(R.id.show);
            hide=(Button)findViewById(R.id.hide);
            viewStub=(ViewStub)findViewById(R.id.viewStub);
            viewStub.inflate();
    
            show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewStub.setVisibility(View.VISIBLE);
                }
            });
            hide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewStub.setVisibility(View.GONE);
                }
            });
        }
    }
    

    輸出

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 日本道免费精品一区二区| 天堂Aⅴ无码一区二区三区| 91精品一区二区综合在线| 精品国产一区二区三区在线 | 无码人妻精品一区二区三区99不卡| 91精品一区二区| 亚洲av鲁丝一区二区三区| 成人精品一区二区三区中文字幕| 婷婷亚洲综合一区二区| 国产一区二区三区小说| 无码日韩人妻av一区免费| 91久久精品国产免费一区| 精品无码AV一区二区三区不卡| 国产一区二区三区免费| 国产色综合一区二区三区| 在线精品动漫一区二区无广告| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 亚洲精品无码一区二区| 久久综合亚洲色一区二区三区| 中文字幕一区二区三区5566| 亚洲美女一区二区三区| 精品视频一区二区三区四区| 国产乱人伦精品一区二区| 日韩一区二区视频在线观看 | 国产综合一区二区| 亚洲av鲁丝一区二区三区| 97久久精品一区二区三区| 精品欧洲av无码一区二区三区| 精品无码一区二区三区在线 | 成人丝袜激情一区二区 | 一区二区三区免费在线视频| AV怡红院一区二区三区| 在线观看午夜亚洲一区| 亚洲色偷偷偷网站色偷一区| 丝袜美腿一区二区三区| 一区二区三区亚洲视频| 看电影来5566一区.二区| 精品国产a∨无码一区二区三区| 国产乱码精品一区二区三区四川人 | 亚洲国产系列一区二区三区 | 极品少妇一区二区三区四区|