我正在尝试根据不同的标准设计活动布局。我已经在xml上创建了布局,默认情况下,它在活动中显示了两个按钮(询问,购买)。
但是,在某些情况下,我不想显示这两个按钮,而只显示其中一个都不位于下方的按钮,完全不同的按钮和不同的操作。我该如何实现?
换句话说,我想使我的新按钮位于购买和询问按钮的中间。具有相同的大小。我该如何实现?现在,并排(购买和询问)不是两个按钮,而是中间只有一个按钮(删除)。
<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"
/>
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
attribute将完全隐藏您的按钮。
搞定此事。
如果要设置三个按钮,请像这样编辑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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句