我正在尝试根据不同的标准设计 Activity 布局。我在 xml 上创建了布局,默认情况下它在 Activity 上显示两个按钮(询问、购买)。
但是,在某些情况下,我不想显示这两个按钮,只显示一个按钮,下面没有一个按钮,完全不同的按钮和不同的操作。我怎样才能实现它?
换句话说,我想让我的新按钮位于买入和卖出按钮的中间。具有相同的大小。我应该如何实现它?以前并排放置两个按钮(购买和询问),现在中间只有一个按钮(删除)。
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
最佳答案
当您想隐藏按钮时,在 Button 对象中使用 setVisibility(View.GONE)
:
Button btnAsk = (Button)findViewById(R.id.btnAsk);
Button btnDel = (Button)findViewById(R.id.btnDel);
Button btnBuy = (Button)findViewById(R.id.btnBuy);
/* if you want to show only Delete button.. */
btnAsk.setVisibility(View.INVISIBLE);
btnDel.setVisibility(View.VISIBLE);
btnBuy.setVisibility(View.INVISIBLE);
/* if you want to show Ask and Buy buttons.. */
btnAsk.setVisibility(View.VISIBLE);
btnDel.setVisibility(View.INVISIBLE);
btnBuy.setVisibility(View.VISIBLE);
View.INVISIBLE
属性将使您的按钮不显示,但它们的位置保持不变。
但是 View.GONE
属性会完全隐藏你的按钮。
照顾好它。
如果你想设置三个按钮,像这样编辑你的xml代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnDel"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
</LinearLayout>
关于android - 隐藏两个按钮并在 xml 上显示不同的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847467/