Listview和TextView对齐

问题描述:

我有一个listView,它从一个rest API服务器获取数据并显示它。我需要在它的侧面添加一个描述数据类型的textView。Listview和TextView对齐

屏幕的右半边有listView,左半边有textView。

问题出现在textView移动以适应不同屏幕尺寸时。我需要将textView固定并与listView一起显示,并与listView保持一条直线,而与屏幕大小无关。干杯。

编辑:图像添加草图

Imgur link

+0

请添加一张草图,显示您的目标是什么 – uguboz

+0

您的XML布局是什么样的? – MidasLefko

+0

草图已被添加为更好的理解,也将添加XML布局。 – MrPool

这听起来像一个ListView是不是你想要使用。您无法将Views与ListView的子级对齐。您需要将TextViews放置在ListView的子项中或不使用ListView。

最简单的方法是使用包含许多水平LinearLayout的垂直LinearLayout。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="100dp" 
      android:layout_height="match_parent"/> 
     <Item that was in the ListView /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="100dp" 
      android:layout_height="match_parent"/> 
     <Item that was in the ListView /> 

    </LinearLayout> 

    ... 
</LinearLayout> 

ListView是真的用于滚动,当你不知道会有多少物品。

也许你已经有了一个适配器,在这个适配器中你正在处理项目的创建,这就是你使用ListView的原因。您仍可以编程方式创建LinearLayout的子项,方法是分别为每个项目分别添加项目并将其添加到容器视图。

原来的答案:

你可能想的TextViews与ListView的滚动以及。所以我认为你应该使用一个ListView,然后将TextView添加到ListView项目的左侧。

+0

ListView只有6个项目,并且不需要滚动。我创建了另一个ListView,但它无法像我需要的那样共享屏幕。我怎样才能追加原始的ListView来显示左边的TextView。干杯。 – MrPool

你应该使用weightSum, 这里是一个例子。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="2" 
    android:orientation="horizontal"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Hello World"/> 

</LinearLayout>