【问题标题】:setVisibility(View.GONE) is not working! Why?setVisibility(View.GONE) 不工作!为什么?
【发布时间】:2020-10-31 23:06:27
【问题描述】:

这段代码在我之前正在开发的应用程序上运行,但现在它根本不会做任何事情,这应该是我在使用这段代码之前必须忘记的一件简单的事情,但我不知道它是什么。

我想要完成的是让按钮消失并使布局可见。

这是我的 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>

这是我的活动:

    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);
        }

}

【问题讨论】:

  • 您永远不会调用您的 addListenerOnButton() 方法,因此永远不会分配 drawbuttondrawlist,并且永远不会设置 OnClickListener
  • 所以 drawbuttonprop 不起作用?
  • 那么分配它们的正确方法是什么?
  • 您调用drawbuttonprop() 的唯一位置是您在addListenerOnButton() 中设置的OnClickListener。不过,您目前没有打电话给addListenerOnButton(),因此永远不会被设置。我不确定您所说的“正确方式”是什么意思,但如果您只是在onCreate() 之后在setContentView() 之后调用您的addListenerOnButton(),那么您所拥有的应该可以工作,假设这是正确的当前布局。
  • 天哪,我知道我忘记了 tnx 伙计,这让我发疯了

标签: java android xml


【解决方案1】:

首先我猜你遇到这种情况的原因是因为你没有调用函数addListenerOnButton(),所以你必须在你的oncreate中调用它,其次你调用的函数也将drawlist可见性设置为可见单击按钮后,但您必须知道单击按钮后您正在尝试查找绘制列表的引用,这将导致应用程序崩溃,因此您需要将这行代码drawlist = (LinearLayout) findViewById(R.id.drawlist); 与第一findViewById 按钮

【讨论】:

    猜你喜欢
    • 2011-11-12
    • 2015-09-10
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2018-09-11
    • 2014-01-30
    相关资源
    最近更新 更多