【问题标题】:The layout's order changes after one button becomes invisible一个按钮变得不可见后布局的顺序发生变化
【发布时间】:2016-11-30 00:55:34
【问题描述】:

这是原始布局:http://prntscr.com/bxods5

它的 XML:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"

    >
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="10">

        <Button
            android:id="@+id/editBtn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="36dp"
            android:layout_column="2"
            android:layout_weight="2"
            android:onClick="editEvent"
            android:text="EDIT" />

        <TextView
            android:id="@+id/attendingEventInListviewRow"
            android:layout_width="0dp"
            android:layout_weight="6"
            android:layout_height="wrap_content"
            android:layout_column="38"
            android:height="30sp"
            android:tag="Events2goName"
            android:text="Events2go"
            android:textColor="#000000"
            android:textSize="22sp"
            android:textStyle="normal"
            android:layout_gravity="right"
            />

        <Button
            android:id="@+id/cancel_arrive"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="36dp"
            android:layout_column="37"
            android:layout_gravity="right"
            android:onClick="selectCancelArrive"
            android:text="Nope" />
    </TableRow>   



</TableLayout>

问题是,并非所有用户都可以编辑,所以对于某些用户,“编辑”按钮会消失,如下所示:

if(resource==R.layout.attending_listview_row) {
        eventName = (TextView) view.findViewById(R.id.attendingEventInListviewRow);
        cancelArrive = (Button) view.findViewById(R.id.cancel_arrive);
        editBtn=(Button)view.findViewById(R.id.editBtn);
        editBtn.setVisibility(View.GONE);
    }
    if (myEvent != null) {
        if (myEvent.getOwner()== LocalDataBase.getCurrentUser()) {
        editBtn.setVisibility(View.VISIBLE);

         }

我仍然希望“NOPE”按钮和 Events2Go 文本视图在它之后留在它们的位置,但实际发生的是:http://prntscr.com/bxoggo

让他们坚守岗位的最佳方式是什么?


编辑:

现在我知道问题在于“GONE”和“INVISIBLE”之间的区别。

【问题讨论】:

    标签: java android button layout


    【解决方案1】:

    我想你现在像这样隐藏editBtn

    editBtn.setVisibility(View.GONE);

    把它改成

    editBtn.setVisibility(View.INVISIBLE);

    让我知道它是否有效,如果没有将检查

    【讨论】:

      【解决方案2】:

      使用相对布局。试试

      <RelativeLayout         
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_toLeftOf="@+id/cancel_arrive"
              android:orientation="horizontal">
      
              <Button
                  android:id="@+id/editBtn"
                  style="?android:attr/buttonStyleSmall"
                  android:layout_width="wrap_content"
                  android:layout_height="36dp"
                  android:onClick="editEvent"
                  android:text="EDIT" />
      
              <TextView
                  android:id="@+id/attendingEventInListviewRow"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:height="30sp"
                  android:tag="Events2goName"
                  android:text="Events2go"
                  android:textColor="#000000"
                  android:textSize="22sp"
                  android:textStyle="normal"
                  android:layout_gravity="right"
                  />
      
          </LinearLayout>
      
          <Button
              android:id="@+id/cancel_arrive"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="36dp"
              android:layout_alignParentRight="true"
              android:onClick="selectCancelArrive"
              android:text="Nope" />
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        • 2020-11-21
        • 2015-08-15
        • 1970-01-01
        • 2018-02-08
        相关资源
        最近更新 更多