visible: 可見(默認值).
invisible: 不可見, 但仍佔用空間.
gone: 完全不可見, 不佔用空間.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Spinner
android:id="@+id/hidenoption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="@+id/hidenlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="可被隱藏"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" test "
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="正常顯示:不會被隱藏"
/>
</LinearLayout>
主代碼, AndroidHidenMenu.java
package com.AndroidHidenMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
public class AndroidHidenMenu extends Activity {
private static final String[] visibility = {"VISIBLE", "INVISIBLE", "GONE"};
private static final int[] visibilitySetting ={ View.VISIBLE, View.INVISIBLE, View.GONE};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hidenOption = (Spinner)findViewById(R.id.hidenoption);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, visibility);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hidenOption.setAdapter(adapter);
final LinearLayout hidenLayout = (LinearLayout)findViewById(R.id.hidenlayout);
hidenOption.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
hidenLayout.setVisibility(visibilitySetting[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
}
沒有留言:
發佈留言