【问题标题】:How to resize the whole listview via toggle button?如何通过切换按钮调整整个列表视图的大小?
【发布时间】:2017-03-20 09:57:11
【问题描述】:

我想显示一个切换按钮,打开/状态放大整个列表视图,关闭/状态将其返回到正常的前一个视图。我已经设法创建了切换按钮,但是当它被按下时它会放大自身而不是列表视图。如何关联切换按钮以打开/关闭整个列表视图?提前致谢。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/tvList"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"/>

<ToggleButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toggle_button"
    android:textOn="Resize On"
    android:textOff="Resize Off"
    android:layout_weight="0.03"
    android:onClick="changeSizeState"/>

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ToggleButton;
import android.view.ViewGroup.LayoutParams;
import static john.android.study.R.id.tvList;



public class MainActivity extends Activity {

    private ListView lvPhone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvPhone = (ListView)findViewById(R.id.tvList);

        String[] values = new String[]{"Nikos", "Yiannis", "Kostas", "Thanasis", "Paulos",};

        ListView lvPhone = (ListView)findViewById(tvList);

        List<PhoneBook> listPhoneBook = new ArrayList<PhoneBook>();

        for (String value : values) {
            PhoneBook entry = new PhoneBook(value);
            listPhoneBook.add(entry);
        }

        PhoneBookAdapter adapter = new PhoneBookAdapter(this,listPhoneBook);
        lvPhone.setAdapter(adapter);
    }
    public void changeSizeState (View view){
        boolean checked = ((ToggleButton)view).isChecked();
        if(checked) {
            LayoutParams params = view.getLayoutParams();
            params.height = 250;
            view.setLayoutParams(params);
        }
    }
}

【问题讨论】:

  • 只使用片段

标签: android listview button toggle


【解决方案1】:

您正在将布局参数设置为 togle 按钮,请在 listview 上尝试:

public void changeSizeState (View view){
        boolean checked = ((ToggleButton)view).isChecked();
        if(checked) {
            LayoutParams params = view.getLayoutParams();
            params.height = 250;
            lvPhone.setLayoutParams(params);
        }
    }

另外你正在onCreate 中创建一个新的listView,修改如下:

lvPhone = (ListView)findViewById(tvList);

希望这会有所帮助。

【讨论】:

  • 感谢您的回答。在 listview 上试试是什么意思?
  • 你想放大listView的高度。所以将布局参数设置为 listview,这就是我的意思。
  • @tahsinRupam(抱歉我的回答延迟)是的!成功了,我按照你说的做了修改,终于搞定了,谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多