首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >隐藏两个按钮并在xml上显示不同的按钮

隐藏两个按钮并在xml上显示不同的按钮
EN

Stack Overflow用户
提问于 2016-01-18 04:44:59
回答 6查看 1.2K关注 0票数 1

我试图根据不同的标准来设计活动布局。我已经在xml上创建了布局,默认情况下它会显示活动上的两个按钮(询问,购买)。

但是,在某些情况下,我不想显示这两个按钮,只显示一个按钮,其中没有一个按钮下面,完全不同的按钮和不同的行动。我怎么能让它发生呢?

换句话说,我想让我的新按钮保持在购买和询问按钮的中间。同样的尺寸。我该怎么做呢?而不是两个按钮并排(购买和询问),现在只有一个按钮(删除)在中心。

代码语言:javascript
运行
复制
          <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"
            />
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-01-18 04:51:36

当您想隐藏按钮时,在Button对象中使用setVisibility(View.GONE)

代码语言:javascript
运行
复制
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代码:

代码语言:javascript
运行
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2016-01-18 04:55:19

若要隐藏按钮,请使用ONEINVISIBLE

代码语言:javascript
运行
复制
Button btnName = (Button)findViewById(R.id.btnName);

btnName.setVisibility(View.GONE);

可视按钮

代码语言:javascript
运行
复制
btnName.setVisibility(View.VISIBLE);

在xml中使用android:gravity="center"

代码语言:javascript
运行
复制
android:centerInParent="true"
票数 1
EN

Stack Overflow用户

发布于 2016-01-18 04:58:09

代码语言:javascript
运行
复制
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:oriantation="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/btnDelete"
            android:layout_width="match_parent"
            android:gravity="center"
            android:textSize="16sp"
            android:layout_height="wrap_content"
            android:text="delete"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:textStyle="bold"
            android:background="@drawable/button_style"
            android:textColor="@color/white"
            android:visibility="gone"
            />

        <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>

on some condition

delete.setVisibility(View.Visible);
  buy.setVisibility(View.Gone);
  action.setVisibility(View.Gone);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34847467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档