<ViewStub android:id="@+id/viewStub1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:text="Click here Show ViewStrub" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="Click here Hide ViewStrub" /> <ViewStub android:id="@+id/viewStub1" android:layout="@layout/mysubtree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="48dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="115dp" android:text="Text From ViewStrub 3" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView3" android:layout_centerHorizontal="true" android:layout_marginBottom="34dp" android:text="Text From ViewStrub 2" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginBottom="19dp" android:text="Text From ViewStrub 1" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewStub; import android.widget.TextView; public class MainActivity extends Activity { boolean click = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // viewStub1 final ViewStub viwStub1 = (ViewStub) findViewById(R.id.viewStub1); // textView1 (Show) TextView txtView1 = (TextView) findViewById(R.id.textView1); txtView1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (click) { viwStub1.inflate(); click = false; } } }); // textView2 (Hide) TextView txtView2 = (TextView) findViewById(R.id.textView2); txtView2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {; // TODO Auto-generated method stub viwStub1.setVisibility(View.INVISIBLE); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท