【问题标题】:Why doesn't setVisibility() work inside of onResumeFragments()?为什么 setVisibility() 在 onResumeFragments() 中不起作用?
【发布时间】:2014-12-02 02:55:18
【问题描述】:

我遇到了一个问题,在我的 onResumeFragments 方法中设置可见性似乎不起作用。这是有问题的活动:

public class MainActivity extends FragmentActivity {

    private ViewGroup activityBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activityBar = (ViewGroup)findViewById(R.id.activity_bar);
    }

    @Override
    protected void onResumeFragments() {
        if (someCondition) {
            activityBar.setVisibility(View.GONE);
        }
    }
}

活动栏如下所示:

<LinearLayout
    android:id="@+id/activity_bar"
    android:layout_height="@dimen/activity_bar_height"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="#AD000000"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical"
    android:visibility="visible"
    >

我已经删除了许多其他细节,但基本上,如果满足给定条件,我想在我的onResumeFragments 中隐藏这个活动栏。我发现setVisibility 似乎没有做任何事情。在调用该行之后,我可以在 activityBar 上调用getVisiblity 并看到它设置为 GONE,但它仍然显示在 UI 中。我也可以将它设置为 Y 并看到它在屏幕上向下移动。我还仔细检查了确保这发生在主线程中。我有点不知道为什么这不起作用以及最好的解决方法是什么。

【问题讨论】:

  • 线性布局活动栏; activityBar = (LinearLayout)findViewById(R.id.activity_bar); // 试一试,让我们知道它是否有效。
  • 我很确定这不会有什么不同,因为LinearLayout 扩展了ViewGroup,但我还是尝试了。结果相同。
  • 感谢您的尝试。是的,几乎每个 gui 元素都有视图。

标签: android


【解决方案1】:

在处理 onStart() 和 onResume() 时,事情会变得有些棘手。例如,您不应该在 FragmentActivity 的 onResume() 方法中提交事务,因为在某些情况下,可以在活动状态恢复之前调用该方法(有关更多信息,请参阅documentation)。如果您的应用程序需要在除 onCreate() 之外的 Activity 生命周期方法中提交事务,请在 FragmentActivity#onResumeFragments() 或 Activity#onPostResume() 中执行。因此,只需尝试修改您的代码,看看这是否适用于 onPostResume()。

【讨论】:

  • 正如我的问题本身所述,它在onResumeFragments() 中不起作用。我刚刚在onPostResume() 中尝试过,它似乎也不在那里工作。
【解决方案2】:

事实证明,在我尝试设置它的可见性之前,activity_bar 已经被动画化了。这张票中包含的信息与我遇到的问题相同,并且有相同的解决方案。

Why doesn't setVisibility work after a view is animated?

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 2019-11-18
    • 2012-01-31
    • 2020-10-31
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多