简体   繁体   English

setVisibility(View.GONE) 不起作用? 为什么?

[英]setVisibility(View.GONE) is not working! Why?

I can swear to anything you like that this code was working on my previous app that I was working on, but now it wont do anything at all, It should be a simple thing I must have forgot before using this code but I dont know what it was.我可以向任何你喜欢的东西发誓,这段代码正在我以前正在开发的应用程序上运行,但现在它根本不会做任何事情,这应该是我在使用这段代码之前必须忘记的一件简单的事情,但我不知道是什么它是。

what I am trying to accomplish is to make the button gone and make the layout visible.我想要完成的是使按钮消失并使布局可见。

here is my xml:这是我的 xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E675E8">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FFFFFF"
        android:layout_marginBottom="2dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1.0">

            <ImageView
                android:src="@drawable/natlilandbarpic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <Button
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:background="@drawable/drawbtn"
            android:id="@+id/drawbutton"
            android:visibility="visible"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:layout_weight="1.0">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Text"
            android:id="@+id/webView"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:gravity="left"
            android:layout_marginLeft="100dp"
            android:id="@+id/drawlist">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

here is my activity:这是我的活动:

    public class MainActivity extends Activity 
    {
        private WebView webView;
        private Button drawbutton;
        private LinearLayout drawlist;
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getActionBar().hide();
            
            
            webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("");
            webView.setWebViewClient(new WebViewClient(){
    
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url){
                        view.loadUrl(url);
                        return true;
                    }
                });
            }
                
        public void addListenerOnButton() {
            
            drawbutton = (Button) findViewById(R.id.drawbutton);
            drawbutton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View p) {   
                    drawbuttonprop(p);
                    }
                });
            drawlist = (LinearLayout) findViewById(R.id.drawlist);
        }
        public void drawbuttonprop(View view)
        {
            drawbutton.setVisibility(View.GONE);
            drawlist.setVisibility(View.VISIBLE);
        }

}

Any kind of help and tricks to make it work I would be appreciate it.任何形式的帮助和技巧使其工作我将不胜感激。

First of All i guess the reason why you re experiencing that because you are not calling the function addListenerOnButton(), so you have to call it in your oncreate, secondly you are calling the function which also sets the drawlist visibility to visible after the button is clicked but you have to be aware that you are trying to find the reference of the drawlist after clicking the button and that will cause the app to crash, so you need to put this line of code "drawlist = (LinearLayout) findViewById(R.id.drawlist);"首先,我猜你遇到这种情况的原因是因为你没有调用 function addListenerOnButton(),所以你必须在你的 oncreate 中调用它,其次你调用 function,它也将绘图列表可见性设置为按钮后可见被点击但你必须知道你在点击按钮后试图找到drawlist的引用,这会导致应用程序崩溃,所以你需要把这行代码“drawlist =(LinearLayout)findViewById(R .id.drawlist);" along with first findViewById of the button, hope this helps.连同按钮的第一个 findViewById 一起,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM