SetVisibility 不适用于 ImageButton

发布于 2024-11-13 23:40:20 字数 388 浏览 3 评论 0原文

因此,我一直在论坛中寻找如何做到这一点,但我发现没有任何效果。当我在图像按钮上调用 setVisibility() 时,该按钮不受影响。下面是 onCreate 方法中的代码,当我运行应用程序时,两个按钮都会显示。但是,如果我将属性硬编码到 xml 文件中,可见性就会发生变化。有什么想法为什么会发生这种情况吗?

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn1 = new ImageButton(this);
    btn1.setVisibility(GONE);
    btn2 = new ImageButton(this);
    btn2.setVisibility(GONE);

So I've been looking around in forums for how to do this, but nothing I have found has worked. When I call setVisibility() on my image button, the button is unaffected. Below is my code that is in the onCreate method, and both buttons are showing up when I run the application. However, if I were to hardcode the attribute into the xml file, the visibility does change. Any ideas why this is happening?

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn1 = new ImageButton(this);
    btn1.setVisibility(GONE);
    btn2 = new ImageButton(this);
    btn2.setVisibility(GONE);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-11-20 23:40:20

将代码更改为:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (ImageButton)findViewById(R.id.btn1);
btn1.setVisibility(View.GONE);
btn2 = (ImageButton)findViewById(R.id.btn2);
btn2.setVisibility(View.GONE);

并修改 main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:gravity="center_horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  >
        <Button android:id="@+id/btn1" android:layout_width="100dip" 
            android:layout_height="40dip" android:text="btn1"/>       
        <Button android:id="@+id/btn2" android:layout_width="100dip" 
            android:layout_height="40dip" android:text="btn2"/>

    </LinearLayout>

Change your code to :

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (ImageButton)findViewById(R.id.btn1);
btn1.setVisibility(View.GONE);
btn2 = (ImageButton)findViewById(R.id.btn2);
btn2.setVisibility(View.GONE);

and modify your main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:gravity="center_horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  >
        <Button android:id="@+id/btn1" android:layout_width="100dip" 
            android:layout_height="40dip" android:text="btn1"/>       
        <Button android:id="@+id/btn2" android:layout_width="100dip" 
            android:layout_height="40dip" android:text="btn2"/>

    </LinearLayout>
尹雨沫 2024-11-20 23:40:20

首先:您应该像这样使用 setVisibility 方法:

btn1.setVisibility(VIEW.GONE),// not setVisibility(GONE);

第二:
您已经创建了按钮,但是您没有将它们添加到您的活动中,(您的活动的内容是(R.layout.main

尝试以下操作:

super.onCreate(savedInstanceState);

    btn1 = new ImageButton(this);
    setContentView(btn1);
    btn1.setVisibility(VIEW.GONE);
    try{
          Thread.sleep(3000);//pause 3 secondes 
    }catch(Exception e){}
    btn1.setVisibility(View.VISIBLE);

编辑:

super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    ImageButton btn1 = new ImageButton(this);
    ImageButton btn2 = new ImageButton(this);
    layout.addView(btn1);
    layout.addView(btn2);
    setContentView(layout);
    btn1.setVisibility(VIEW.GONE);
    btn2.setVisibility(VIEW.VISIBLE);
    try{
          Thread.sleep(3000);//pause 3 secondes 
    }catch(Exception e){}
    btn1.setVisibility(View.VISIBLE);
    btn2.setVisibility(VIEW.GONE);

FIRST : you shoud use the method setVisibility like this :

btn1.setVisibility(VIEW.GONE),// not setVisibility(GONE);

SECONDE:
you have created your buttons , but you didn't add them to your activity , ( the content of your activity is ( R.layout.main )

try this :

super.onCreate(savedInstanceState);

    btn1 = new ImageButton(this);
    setContentView(btn1);
    btn1.setVisibility(VIEW.GONE);
    try{
          Thread.sleep(3000);//pause 3 secondes 
    }catch(Exception e){}
    btn1.setVisibility(View.VISIBLE);

EDIT :

super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    ImageButton btn1 = new ImageButton(this);
    ImageButton btn2 = new ImageButton(this);
    layout.addView(btn1);
    layout.addView(btn2);
    setContentView(layout);
    btn1.setVisibility(VIEW.GONE);
    btn2.setVisibility(VIEW.VISIBLE);
    try{
          Thread.sleep(3000);//pause 3 secondes 
    }catch(Exception e){}
    btn1.setVisibility(View.VISIBLE);
    btn2.setVisibility(VIEW.GONE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文