【问题标题】:Android Show Activity Title/status bar at the top after it is hiddenAndroid 隐藏后在顶部显示活动标题/状态栏
【发布时间】:2013-03-15 16:43:41
【问题描述】:

目前在我的FragmentActivity 中,我通过onCreate 方法隐藏状态栏,执行以下操作:

 requestWindowFeature(Window.FEATURE_NO_TITLE);
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

这没问题。

但是在全屏下,假设用户点击一个按钮,我会想要换入另一个片段(记住我们在FragmentActivity),我的意思是替换当前全屏显示的片段。

但我希望显示标题栏/状态。

这可能吗?如果是这样,我该如何以编程方式进行操作

【问题讨论】:

    标签: android android-activity statusbar


    【解决方案1】:

    在这里,您可以使用以下两种方法动态更改标题栏。我用我的Activity 给他们打电话。因此,要从 Fragment 调用,您需要 Activity 实例。

    public void hideTitle() {
            try {
                ((View) findViewById(android.R.id.title).getParent())
                        .setVisibility(View.GONE);
            } catch (Exception e) {
            }
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
    
        public void showTitle() {
            try {
                ((View) findViewById(android.R.id.title).getParent())
                        .setVisibility(View.VISIBLE);
            } catch (Exception e) {
            }
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    

    【讨论】:

    • 太棒了!!!!为你的精彩回答喝彩!!!它应该是公认的答案!
    • 感谢@Rudi .. 当我得到这种灵感时,回答变得有意义:)
    【解决方案2】:

    有几种方法可以这样做:

    第一种方法:

    FEATURE_CUSTOM_TITLE

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.foo_layout);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 
    or
    
    youractivity.setTitle();
    

    注意!您可以在布局中包含一个简单的TextView custom_title_bar

    让你custom_title_bar布局如下:

       <LinearLayout           
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
          <TextView
             android:id="@+id/titleTextView"
             style="@android:style/WindowTitle"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="TextView"
          />
        </LinearLayout>
    

    第二种方法:

    Activity.setTitle

    this.setTitle("My Title!");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 2017-09-16
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多