那么我想要做的是创建一个初始布局,将有一个切换按钮,并单击切换,但它应该使框架可见,其中将有几个按钮或textviews。使框架的组件只有在需要时才可见android
有谁知道如何在Android 2.2中做到这一点?
那么我想要做的是创建一个初始布局,将有一个切换按钮,并单击切换,但它应该使框架可见,其中将有几个按钮或textviews。使框架的组件只有在需要时才可见android
有谁知道如何在Android 2.2中做到这一点?
您可以使用一个视图visibility
属性来控制它是否可见或不可见。这里有一个小例子,应该做你想找的。
主要Activity
:
public class DynamicLayoutTestActivity extends Activity {
private ToggleButton toggleButton;
private View possiblyHiddenView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
possiblyHiddenView = (View) findViewById(R.id.possiblyHiddenView);
toggleButton.setOnCheckedChangeListener(toggleButtonOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener toggleButtonOnCheckedChangeListener
= new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
possiblyHiddenView.setVisibility(View.VISIBLE);
} else {
possiblyHiddenView.setVisibility(View.INVISIBLE);
}
}
};
}
布局文件,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"
>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Show"
android:textOn="Hide" />
<LinearLayout
android:id="@+id/possiblyHiddenView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
>
<TextView
android:text="Stuff that could be hidden."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
如果你不想隐藏视图占用任何空间,使用可视性gone
代替invisible
。
希望这会有所帮助!
开发defintly帮助你的感谢,但是当它变得可见它仍然与其他视图放置游手好闲。我真正想要的是在现有组件的顶部可以看到的东西,当不需要时可以离开 – LostPuppy
您的意思就像是一个对话框? –
是的,但它应该有控制,我的意思是可以点击不只是一个好的或取消按钮 – LostPuppy