我想显示一个切换按钮,使用On / state放大整个列表视图,并使用Off / state将其返回到正常的上一个视图。我已经设法创建了切换按钮但是当它被按下时它会扩大自己而不是列表视图。如何关联切换按钮以放大/关闭整个列表视图?提前谢谢。
<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);
}
}
}
答案 0 :(得分:1)
您要将布局参数设置为toogle按钮,请在listview
上尝试:
public void changeSizeState (View view){
boolean checked = ((ToggleButton)view).isChecked();
if(checked) {
LayoutParams params = view.getLayoutParams();
params.height = 250;
lvPhone.setLayoutParams(params);
}
}
此外,您正在listView
中发起新的onCreate
,修改如下:
lvPhone = (ListView)findViewById(tvList);
希望这有帮助。