Android列表下面切换按钮

时间:2013-08-12 21:59:36

标签: android

我有一个列表,旨在位于切换按钮下方。该列表从服务器获取数据,然后解析它们。我的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="wrap_content"
    android:orientation="horizontal" >

    <ToggleButton
        android:id="@+id/toggle_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textOff="Apps"
        android:textOn="Apps" />

    <ToggleButton
        android:id="@+id/toggle_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/toggle_button1"
        android:layout_weight="1"
        android:textOff="VMs"
        android:textOn="VMs" />

    <ToggleButton
        android:id="@+id/toggle_button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/toggle_button2"
        android:layout_weight="1"
        android:textOff="Groups"
        android:textOn="Groups" />

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toggle_button1" />
</RelativeLayout>

实际片段的代码:

public class ProblemFragment extends SherlockListFragment
{
    private SeparatedListAdapter list;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getSherlockActivity().setContentView(R.layout.problem_layout);

        list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
        ToggleButton b1 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button1);
        ToggleButton b2 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button2);
        ToggleButton b3 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button3);

        setListAdapter(list);
        refresh();
    }

    public void refresh()
    {
        list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
        refreshStats();
    }

    public void refreshStats()
    {
        //Omitted parsing code

        list.addSection(new String("Hello world!!"));
        setListAdapter(list);
    }
}

但是,当我使用setListAdapter(list)时,按钮会被覆盖。它们在应用程序检索数据并解析之前可见,但在调用setListAdapter后会被覆盖。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

首先,删除

 android:orientation="horizontal"

来自您的根layoutRelativeLayout没有orientation属性。另外,weight适用于LinearLayout的子元素,当您使用它时,您应该将每个子视图的width分配给0dp以获得水平orientation和垂直height="0dp"的{​​{1}}。

然后将orientation包裹在ToggleButtons,纵向或横向LinearLayout中,并将其归属于

orientation

然后给你的android:layout_alignParentTop="true" 财产

ListView

所以它可能看起来像

android:layout_below="@id/idOfLinearLayout"

我还从 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/toggleLL" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true"> <ToggleButton android:id="@+id/toggle_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="Apps" android:textOn="Apps" /> <ToggleButton android:id="@+id/toggle_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="VMs" android:textOn="VMs" /> <ToggleButton android:id="@+id/toggle_button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="Groups" android:textOn="Groups" /> </LinearLayout> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/toggleLL" /> </RelativeLayout> 删除了RelativeLayout属性,因为它们现在包含在ToggleButtons中。并且你有一个圆形视图错误,将第二个LinearLayout分配给它自己的右边,这可能是一个复制/粘贴错误。希望这会有所帮助。

注意 ToggleButton的默认orientationLinearLayout,因此保留该属性会产生这种效果。

答案 1 :(得分:0)

哦!我无法测试您的XML,但我认为您需要滚动条!如果列表中填充了大量条目,则屏幕会变得更大,使按钮消失,因为它们被列表推送。尝试将滚动添加到整个布局。

这样的事情:

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

               <!-- Original layout here -->

   </ScrollView>
</LinearLayout>

当然,如果您只在scrollview中放置一个布局,则不需要外部布局:

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

               <!-- Original layout here -->

</ScrollView>