Senin, 13 Januari 2014

ViewStub

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.
ViewStub
ViewStub

Main layout, to include <ViewStub>.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<ViewStub
android:id="@+id/mystub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/mytitle"
android:layout="@layout/titlebar" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show ViewStub" />

</LinearLayout>

titlebar.xml, to be inflated in <ViewStub>.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/background_dark">

<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Android-Coding"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="http://android-coding.blogspot.com/"
android:textColor="@android:color/white"
android:textStyle="italic"/>

</LinearLayout>
</LinearLayout>

Inflate the ViewStub in java code.
package com.example.androidviewstub;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

ViewStub myViewStub;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myViewStub = (ViewStub) findViewById(R.id.mystub);

Button buttonShow = (Button)findViewById(R.id.show);
buttonShow.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
if(myViewStub.getParent() != null){
myViewStub.inflate();
}else{
Toast.makeText(MainActivity.this,
"myViewStub.getParent() == null: replaced",
Toast.LENGTH_LONG).show();
}

}});
}

}

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved