【问题标题】:Stack in Hiding and unhiding layout in Android Studio在 Android Studio 中隐藏和取消隐藏布局中的堆栈
【发布时间】:2018-08-12 16:18:07
【问题描述】:

我正在为我的应用程序而苦苦挣扎。它应该像下面这样工作。

每次我点击按钮时,它都会隐藏 id 为 pic1 的布局,如果它已经隐藏,当我再次点击按钮时它会取消隐藏。

但是,问题是每次我点击它,它都会回到之前的Activity

这是我的布局中的 2 个布局和一个按钮。

<LinearLayout
    android:id="@+id/backpic"
    android:layout_width="350dp"
    android:layout_height="425dp"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_marginTop="70dp"
    android:background="@drawable/back" />

<LinearLayout
    android:id="@+id/frontpic"
    android:layout_width="350dp"
    android:layout_height="425dp"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_marginTop="70dp"
    android:background="@drawable/front" />

<Button
    android:id="@+id/btnRotate"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="20dp"
    android:background="@drawable/rotate"
    android:onClick="rotate"/>

如果按钮被点击,它将执行函数rotate()

public class MusclesActivity extends AppCompatActivity {
    Button btnRotate;
    LinearLayout pic1,pic2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

         btnRotate = (Button)findViewById(R.id.btnRotate);
         pic1 = (LinearLayout)findViewById(R.id.frontpic);
         pic2 = (LinearLayout)findViewById(R.id.backpic);

        setContentView(R.layout.activity_muscles);
    }
}

而函数rotate是

public void rotate(View views) {
    if (pic1.getVisibility()==View.VISIBLE)
        pic1.setVisibility(View.INVISIBLE);
    else
        pic1.setVisibility(View.VISIBLE);
}

我在这里做错了什么?

【问题讨论】:

  • 显示调用旋转的代码。我的猜测是你每次都叫旋转两次。您可能想在旋转代码中添加打印跟踪。
  • 你能把代码贴出来吗?如果它没有按返回按钮或以编程方式返回之前的活动,那就有点奇怪了。
  • 点击代码在哪里?

标签: java android xml visibility


【解决方案1】:

尝试下面的代码一次,如果它有效,请告诉我。

更新

public class MusclesActivity extends AppCompatActivity {
Button btnRotate;
LinearLayout pic1,pic2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.activity_muscles); // place it here


 btnRotate = (Button)findViewById(R.id.btnRotate);
 pic1 = (LinearLayout)findViewById(R.id.frontpic);
 pic2 = (LinearLayout)findViewById(R.id.backpic);
}

【讨论】:

  • 还是回到之前的Activity。
  • 显示您的完整课程代码,您正在处理 onClick。
  • 一切尽在掌握。
  • @GabrielMamites 我已经更新了我的答案检查并告诉我。
  • 谢谢大家,我已经解决了。我刚刚做了这个public void rotate(View views) { btnRotate = (Button)findViewById(R.id.btnRotate); pic1 = (LinearLayout)findViewById(R.id.frontpic); pic2 = (LinearLayout)findViewById(R.id.backpic); if (pic1.getVisibility()==View.VISIBLE){ pic1.setVisibility(View.INVISIBLE); Toast.makeText(MusclesActivity.this, "VISIBLE", Toast.LENGTH_SHORT).show(); } else pic1.setVisibility(View.VISIBLE); }
猜你喜欢
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2021-09-27
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
相关资源
最近更新 更多