Android 一个活动中有两个列表视图(未显示)
我试图显示两个列表视图,一个在另一个下面。问题是第一个是隐藏的,或者没有显示出来 我怎么做?内部带有相对线布局的滚动视图,还是线性布局? 非常感谢 这是我的布局代码和屏幕截图:Android 一个活动中有两个列表视图(未显示),android,listview,Android,Listview,我试图显示两个列表视图,一个在另一个下面。问题是第一个是隐藏的,或者没有显示出来 我怎么做?内部带有相对线布局的滚动视图,还是线性布局? 非常感谢 这是我的布局代码和屏幕截图: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" andr
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_header_selector"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/header_solicituds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingLeft="25dp"
android:paddingTop="8dp"
android:text="TextView"
android:textColor="@android:color/primary_text_dark"
android:textSize="20sp" />
<View
android:id="@+id/separator_solicituds"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="@android:color/holo_orange_light"
android:visibility="visible" />
<ListView
android:id="@+id/listView_solicituds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#cdd81e"
android:dividerHeight="0.1dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="20dip"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<TextView
android:id="@+id/header_amics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingLeft="25dp"
android:paddingTop="8dp"
android:text="TextView"
android:textColor="@android:color/primary_text_dark"
android:textSize="20sp" />
<View
android:id="@+id/separator"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#deeb0e"
android:visibility="visible" />
<ListView
android:id="@+id/listView_amics"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#cdd81e"
android:dividerHeight="0.1dp"
android:scrollX="0px"
android:scrollY="0px"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="20dip"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="No friends added yet."
android:textSize="20dp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
我们不应该将listview放在scrollview中。感染我们不应该在滚动视图中放置任何滚动视图。如果这样做,则您的内部滚动将无法获得焦点,或者很难滚动。尝试使用相对布局 或者为列表视图指定权重,以便它们可以占用linearlayout中的空间
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
如何在一个活动中使用两个列表视图的问题困扰了我很长时间,我不愿意承认。以下是我最终解决问题的方法。其实很简单 您需要将两个列表视图都粘贴到一个scrollview中,就像您尝试的那样。现在,通常在执行此操作时,listview只呈现一行的大小,该行在列表中滚动。这显然不是最优的。因此,解决方案是根据列表中的项目/行数,通过编程调整两个列表视图的布局高度。下面是一个如何工作的示例: XML: 如果您真的想使用dp动态设置listview的高度,那么您需要转换像素大小
final float scale = getResources().getDisplayMetrics().density;
int rowHeight = (int) (dp_height_of_your_rows * scale + 0.5);
然后,不要插入整数,而是使用rowHeight*listView中的行数。尝试不使用滚动视图。scrollview和listview之间存在一些问题,这些问题无法正常工作。为每个listview添加2个LinearLayout或RelativeLayout。如果你使用linear,你可以使用android:layout_weight=“1”为每个ListView提供一半的屏幕。scrollview+ListView,romain guy会吃掉你的。不要使用scroll view,为listview和textview使用线性布局,并根据您的要求将其设置为布局权重。这将很好地工作。我遇到了一个问题:您必须在线性布局视图中使用权重来解决它示例:在listview和线性布局的其他子视图中添加这些属性android:layout\u height=“0dp”android:layout\u weight=“4”
ListView list1 = (ListView) findViewById(R.id.exp_listView1);
list1.getLayoutParams().height = ***set the desired height of list1 using an int***;
ListView lsit2 = (ListView) findViewById(R.id.listView2);
list2.getLayoutParams().height = ***set the desired height of list2 using an int***;
final float scale = getResources().getDisplayMetrics().density;
int rowHeight = (int) (dp_height_of_your_rows * scale + 0.5);