如何在按钮单击上隐藏布局。下面是我的布局。点击搜索按钮我需要隐藏家庭布局并显示搜索布局,反之亦然。
<com.bibliotheque.android.view.viewgroup.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444488"
android:orientation="vertical" >
<Button
android:id="@+id/home_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="@string/home_btn_label" />
<Button
android:id="@+id/search_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="@string/search_btn_label" />
<Button
android:id="@+id/mybooks_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="@string/mybooks_btn_label" />
<Button
android:id="@+id/contact_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="@string/contact_btn_label" />
<Button
android:id="@+id/about_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="toggleMenu"
android:text="@string/about_btn_label" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#888888"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/home"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#888888"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#888888"
android:orientation="vertical" >
<EditText
android:id="@+id/searchInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="text" />
<ListView
android:id="@+id/bookList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</com.bibliotheque.android.view.viewgroup.FlyOutContainer>
我试过如下
public class MainActivity extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
FlyOutContainer root;
private ListView lv;
Button home,search,mybooks,contact,about;
View home_layout,search_layout,mybooks_layout,contact_layout,about_layout;
// Listview Adapter
ArrayAdapter<String> adapter;
EditText inputSearch;
ViewGroup content;
ArrayList<HashMap<String, String>> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
home=(Button)findViewById(R.id.home_btn);
search=(Button)findViewById(R.id.search_btn);
mybooks=(Button)findViewById(R.id.mybooks_btn);
contact=(Button)findViewById(R.id.contact_btn);
about=(Button)findViewById(R.id.about_btn);
home_layout=findViewById(R.id.home);
search_layout=findViewById(R.id.search_layout);
//mybooks_layout=(LinearLayout)findViewById(R.id.mybooks_layout);
//contact_layout=(LinearLayout)findViewById(R.id.contact_layout);
//about_layout=(LinearLayout)findViewById(R.id.about_layout);
detector = new SimpleGestureFilter(this,this);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null);
this.setContentView(root);
inputSearch=(EditText)findViewById(R.id.searchInput);
lv=(ListView)findViewById(R.id.bookList);
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
adapter = new ArrayAdapter<String>(this, R.layout.book_list_item, R.id.book_item, products);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample, menu);
return true;
}
public void toggleMenu(View v){
Log.i("Toggle method", "Toggle Method");
home_layout.setVisibility(View.GONE); // not working
this.root.toggleMenu();
}
public enum MenuState {
CLOSED, OPEN, CLOSING, OPENING
};
@Override
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT :
if(this.root.menuCurrentState==com.bibliotheque.android.view.viewgroup.FlyOutContainer.MenuState.CLOSED){
try{
this.root.toggleMenu();
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
break;
case SimpleGestureFilter.SWIPE_LEFT :
if(this.root.menuCurrentState==com.bibliotheque.android.view.viewgroup.FlyOutContainer.MenuState.OPEN){
try{
this.root.toggleMenu();
}catch(Exception e){
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
break;
case SimpleGestureFilter.SWIPE_DOWN :
break;
case SimpleGestureFilter.SWIPE_UP :
break;
}
//Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onDoubleTap() {
// TODO Auto-generated method stub
}
}
永远不会调用toggleMenu方法中的setVisibility(View.GONE)或不工作。怎么能 我隐藏那个布局?
我在我的另一个基本应用程序中尝试相同的代码,它工作正常但不在这里工作。:(我无法弄清楚问题。请帮助我。
答案 0 :(得分:1)
你没有告诉你的布局消失:
home.setVisibility(View.GONE);//not working
,而是由:
引用的按钮home=(Button)findViewById(R.id.home_btn);
答案 1 :(得分:1)
不确定为什么要使用布局隐藏/显示而不是对话框(假设您希望调出搜索框以输入搜索条件),但无论如何,只需尝试快速基本按钮点击 - &gt;切换布局。
home_ly是需要在加载活动中显示的标准布局。 new_ly是您希望加载的新布局。将此设置为默认隐藏。
private void ChangeLayout(){
LinearLayout home_ly = (LinearLayout)findViewById(R.id.home_layout);
LinearLayout new_ly = (LinearLayout)findViewById(R.id.new_layout);
if(home_ly.getVisibility() == View.VISIBLE && new_ly.getVisibility() == View.INVISIBLE){
home_ly.setVisibility(View.INVISIBLE);
new_ly.setVisibility(View.VISIBLE);
}
else if(home_ly.getVisibility() == View.INVISIBLE && new_ly.getVisibility() == View.VISIBLE){
home_ly.setVisibility(View.VISIBLE);
new_ly.setVisibility(View.INVISIBLE);
}
else{
home_ly.setVisibility(View.VISIBLE);
new_ly.setVisibility(View.INVISIBLE);
}
}
这是oncreate:
Button btn = (Button)findViewById(R.id.change);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ChangeLayout();
}
});