在Android Studio中隐藏和取消隐藏布局

问题描述 投票:1回答:1

我正在努力应对我的应用。它应该像下面这样工作。

每次我点击按钮它都会隐藏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);
    }
}

而旋转的功能是

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

我在这做错了什么?

java android xml visibility
1个回答
0
投票

尝试下面的代码一次,让我知道它是否有效。

UPDATE

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.