我试图在按下按钮时显示LinearLayout。我无法调用menuLayout.setVisibility(menuLayout.VISIBLE)方法,每次都会出现错误。它确实要求将其转换为Kotlin:menuLayout.visibility = menuLayout.VISIBLE,但仍然无法正常工作。

ExamActivity.kt

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_exam.*

class ExamActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_exam)

        val menuLayout: LinearLayout = this.LinearLayoutMenu

        val menuBtn: Button = this.menuBtn

        menuBtn.setOnClickListener {
            menuLayout.visibility = menuLayout.VISIBLE
            menuLayout.setVisibility(menuLayout.VISIBLE)
        }
    }
}


activity_exam.xml

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ExamActivity"
        android:background="@color/colorGrayBgd">
    <LinearLayout
            <-- hidden to save time -->
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/LinearLayoutMenu"
            android:layout_marginHorizontal="24dp"
            android:weightSum="3"
            android:gravity="center" android:visibility="gone" tools:layout_editor_absoluteX="24dp">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:gravity="center"
                android:weightSum="100">
            <TextView
                    android:text="PAUSED"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/titleTextView"
                    android:layout_weight="20"
                    android:textSize="46dp"
                    android:gravity="center"/>
            <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/separatorView"
                    android:layout_weight="3"
                    android:background="@android:color/black"/>
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/LinearLayoutTimeQuestionNumber"
                    android:layout_weight="15">
                <TextView
                        android:text="Time Remaining"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/timeTextView" android:layout_weight="1"
                        android:textSize="16dp"
                        android:gravity="center"/>
                <TextView
                        android:text="Questions Remaining"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/questionNumberTextView"
                        android:layout_weight="1"
                        android:textSize="16dp"
                        android:gravity="center"/>
            </LinearLayout>
            <Button
                    android:text="Report Question"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/reportQuestion"
                    android:layout_weight="20"
                    android:textSize="24dp"/>
            <Button
                    android:text="Quit"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/quitButton"
                    android:layout_weight="20"
                    android:textSize="24dp"/>
            <Button
                    android:text="Resume"
                    android:layout_width="match_parent"
                    android:layout_height="0dp" android:id="@+id/resumeButton"
                    android:layout_weight="20"
                    android:textSize="24dp"/>
        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>


我期望将LinearLayoutMenu设置为可见,但是我拥有的代码将无法工作。

最佳答案

使用menuLayout.visibility=View.VISIBLE可见,使用menuLayout.visibility=View.GONE隐藏或隐藏

07-26 08:23