【问题标题】:how to visible layout in android如何在android中显示布局
【发布时间】:2016-06-10 16:15:03
【问题描述】:

我正在开发 android 应用程序,其中有两个活动,即主要活动和模式活动。我的第一个活动是主要活动有一些不可见的图标, 第二个活动是模式活动。当我单击第二个活动 中的按钮时,我返回我的第一个活动,我希望那个不可见的图标应该可见。

enter code here

  pro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            autobtn.setImageResource(R.drawable.auyto);
            pro.setImageResource(R.drawable.proactiv);
            Intent it = new Intent(ModeActivity.this, Mainactivity.class);
            startActivity(it);
        }
    });

【问题讨论】:

    标签: android android-fragments sharedpreferences visibility


    【解决方案1】:

    您可以使用隐藏视图

    yourView.setVisibility(View.GONE);
    

    您可以使用 findViewByID 获取对您的视图的引用 :) 当你想找回它时,只需说yourView.setVisibility(View.VISIBLE);

    根据您的问题,在模式活动的 onCreate 或 onStart 中,您可以隐藏不可见的图标,例如

        @Override
        protected void onStart(){
            super.onStart();
            yourView.setVisibility(View.GONE);
        }
    

    在模式活动按钮的 onClickListener 中,您可以使按钮可见。在按钮的 onclickListener 中放置代码

       yourView.setVisibility(View.VISIBLE); 
    

    仅此而已。如果您希望图标在同一按钮上的交替点击时显示和隐藏(使用类似切换的按钮),请输入以下代码

     if (yourView.getVisibility() == View.VISIBLE) {
          yourView.setVisibility(View.GONE); 
      } else {
          // Either gone or invisible
          yourView.setVisibility(View.VISIBLE); 
      }
    

    希望对你有帮助:)

    【讨论】:

    • 我在哪里可以在主要活动中使用这些代码是 onResume()
    • 你可以把它放在我已经展示过的 onStart 或你想要的任何地方的 onCreate 中 :) 只需确保在调用 yourView.setVisibility(View.VISIBLE); 之前获得对图标的引用。您的视图是您的图标:)
    • 每次你的主要活动被加载 onStart() 并且 onCreate 在你的情况下被调用:) 所以可以把它放在任何这个方法中:) 你想把它放在 onResume 中的任何具体原因吗? ?它也应该在那里工作正常:)
    • @Sandeep Bhandari 假设我在专业模式下点击了专业按钮,它返回到主要活动,隐藏按钮应该像图片中的红色方块一样可见
    • 真的很抱歉先生,我只是糊涂了。我之前的回答完全适合您的情况。希望您仍然拥有代码,并且根据您的问题,modeVal 是什么,它是一个整数变量,您将通过添加额外的值来传递其值,就像我之前展示的那样。根据该值,您可以决定您是来自模式活动还是直接进入主要活动:)
    【解决方案2】:

    要使您的图标可见,您可以使用

    icon.setVisibility(View.VISIBLE);
    

    【讨论】:

      【解决方案3】:

      我认为您正在使用 SurfaceView ,如果是,则将 SurfaceView 保留在 FrameLayout 中,并在 SurfaceView 上放置另一个 View

      这样

       <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <SurfaceView
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              android:gravity="bottom"
              android:orientation="horizontal"
              >
      
              <ImageButton
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:src="@drawable/ic_logo"/>
      
              <ImageButton
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:src="@drawable/ic_logo"/>
      
              <ImageButton
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:src="@drawable/ic_logo"/>
      
      
          </LinearLayout>
      
      
      </FrameLayout>
      

      【讨论】:

      • 只需将您的 SurfaceView 与其他视图重叠
      • 没关系,请看我在第一张图片中发布的图片隐藏按钮,当我在第二张图片中单击专业版时,我返回第一个活动,我希望这些按钮显示在红色方块中就是这样
      • 试试这个...只需将 ic_logo 替换为您的图片
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多