如何设置setContentView();去了

时间:2017-06-07 01:48:17

标签: java android visibility

我的主要布局是setContentView(R.layout.activity_detail),

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

如何在以下代码中将布局的可见性设置为GONE

   if (extras.get("NAME").equals("Blog")) {
        Intent blog = new Intent(DetailActivity.this, ReportActivity.class);
        blog.putExtra("URL", "url");
        blog.putExtra("filename", "Blog");
        startActivity(blog);

  // I want to Perform the operation here

        }

4 个答案:

答案 0 :(得分:1)

我认为您实际上是在尝试关闭当前活动,同时打开一个新活动。

您可以按照以下方式执行此操作,无需隐藏当前活动。

 Intent blog = new Intent(DetailActivity.this, ReportActivity.class);
 blog .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 blog.putExtra("URL", "url");
 blog.putExtra("filename", "Blog");
 startActivity(blog);
 finish();

答案 1 :(得分:0)

请按照以下方式进行操作。

if (extras.get("NAME").equals("Blog")) {
    Intent blog = new Intent(DetailActivity.this, ReportActivity.class);
    blog.putExtra("URL", "url");
    blog.putExtra("filename", "Blog");
    startActivity(blog);

    // I want to Perform the operation here...

    //Get the root view and hide...

    findViewById(android.R.id.content).setVisibility(View.GONE);
}

答案 2 :(得分:0)

试试这段代码:

1

 View view=findViewById(android.R.id.content)
 view.setVisibility(View.GONE);

2

View view= getWindow().getDecorView().findViewById(android.R.id.content)
view.setVisibility(View.GONE);

3

final  View view= = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);
view.setVisibility(View.GONE);

4

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/root_view" >

   //...


</LinearLayout>

View view=findViewById(android.R.id.root_view)
view.setVisibility(View.GONE);

答案 3 :(得分:0)

在else部分

中设置内容视图
   if (extras.get("NAME").equals("Blog")) {
    Intent blog = new Intent(DetailActivity.this, ReportActivity.class);
    blog.putExtra("URL", "url");
    blog.putExtra("filename", "Blog");
    startActivity(blog);

    }else{
        setContentView(R.layout.activity_detail);
    }