【问题标题】:how to fill the empty space with ListView after hiding a button隐藏按钮后如何用ListView填充空白空间
【发布时间】:2012-06-18 08:29:54
【问题描述】:

我有一个简单的垂直LinearLayout,其中顶部有一个button,下面有一个Listview
我想要的是,当按下button 时,它会隐藏(使用View.GONE)并且它生成的空白空间由ListView 填充。
但是经过这么多的努力,还是没能实现。

我尝试了invalidate()onDraw(),我尝试将layout_weight=1 赋予ListView,我也尝试了forceLayout()requestLayout() 方法,但它们都不起作用。

奇怪的是,如果有EditTextTextView 或任何其他组件而不是ListView,它的工作正常,即Edittext 等去抓取隐藏Button 生成的空白空间.但在 ListView 的情况下,它不会发生。

【问题讨论】:

  • @user370305 : 那么 EditText 等怎么会去填补那个空间呢?

标签: android android-layout android-listview android-button


【解决方案1】:

这东西在我的模拟器中完美运行

    btn1 = (Button) findViewById(R.id.button1);

    btn1.setOnClickListener( new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            new ProgressTask().execute();
            btn1.setVisibility(8);
        }
    })

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/itemlist"
        android:layout_below="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

【讨论】:

  • 您隐藏的按钮下方是否有 ListView?顺便说一句,这里的“8”是什么?
  • 我正在尝试。无论如何谢谢你的回复。
【解决方案2】:

检查一下……

     public class TestingActivity extends ListActivity {
/** Called when the activity is first created. */
Button checkBtn;
     String[] temp={"item 1","item 2","item 3"};
/** Called when the activity is first created. */

@SuppressWarnings("unchecked")
@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

        getListView().setAdapter(new  ArrayAdapter(this, android.R.layout.simple_list_item_1, temp));
      checkBtn = (Button) findViewById(R.id.button1);

      checkBtn.setOnClickListener(new View.OnClickListener()
      {

       public void onClick(View v)
       {


         checkBtn.setVisibility(View.GONE);


       }});

     } }

xml 是:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="check connetion" />

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollingCache="false" >
</ListView>

  </LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多