【问题标题】:Weird behavior with buttons and visibility gone按钮和可见性消失的奇怪行为
【发布时间】:2014-04-04 11:44:33
【问题描述】:

我在按钮、垂直居中和可见性消失方面遇到了一个非常奇怪的问题。在我的应用程序中,我有一个书籍列表,我正在使用 swipelistview 库在每一行后面创建按钮。我有 3 个按钮:返回、发送提醒和删除。返回和发送提醒按钮的可见性设置为 Visibility.GONE 如果该书没有被借出,则动态设置。现在这是我的问题。我在行的后面有以下 xml 布局。

<LinearLayout
        android:id="@+id/swipelist_backview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:tag="swipelist_backview"
        android:background="#101010">
                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55px"
                android:layout_gravity="center_vertical"
                android:id="@+id/swipe_button1"
                android:text="@string/markAsReturned"/>

                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/swipe_button2"
                android:layout_gravity="center_vertical"
                android:text="@string/sendReminder"/>

                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:id="@+id/swipe_button3"
                android:layout_gravity="center_vertical"
                android:text="@string/delete"/>


</LinearLayout>

现在预期的结果是按钮将垂直居中在每行的中间,如果图书被借出,并且所有按钮都可见,则确实会发生这种情况。但是,如果这本书没有借出,则显示的唯一按钮是删除,并且没有对齐。

我还使用以下代码将删除按钮的左边距设置为 55 像素:

if(!item.isLended())
{
    btnReturn.setVisibility(View.GONE);
    btnSendReminder.setVisibility(View.GONE);
    LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
    lp.setMargins(55, 0, 0, 0);
    btnDelete.setLayoutParams(lp);
}

我认为这可能会删除垂直对齐,但LayoutParams 似乎没有办法设置layout-gravity,所以看起来不像。

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    嗯,我猜是LayoutParams。如果有人有兴趣,我是这样解决的:

    if(!item.isLended())
    {
        btnReturn.setVisibility(View.GONE);
        btnSendReminder.setVisibility(View.GONE);
        LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
        lp.setMargins(55, 0, 0, 0);
        lp.gravity = Gravity.CENTER_VERTICAL;
        btnDelete.setLayoutParams(lp);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多