简体   繁体   English

如何将RelativeLayout设置为ListView?

[英]How to Set the RelativeLayout to ListView?

As we usually see, the listview is rendered line by line. 正如我们通常看到的那样,listview是逐行呈现的。

But I want to set its items anywhere in it by setting the item's left and top. 但是我想通过设置项目的左侧和顶部在其任何位置设置其项目。 And first of all, I think the listview require another layout such as relativelayout.right? 首先,我认为listview需要其他布局,例如relativelayout.right吗? so my question is can we Set a RelativeLayout to ListView? 所以我的问题是我们可以将RelativeLayout设置为ListView吗? how? 怎么样?

My code is 我的代码是

listView.setLayoutParams(new RelativeLayout.LayoutParams(300, 300));

unfortunately,it doesn't work! 不幸的是,它不起作用!

but the code below works fine: 但是下面的代码可以正常工作:

listView.setLayoutParams(new LinearLayout.LayoutParams(300, 300));

Can Somebody tell me what's wrong with it? 有人可以告诉我这是怎么回事吗? Is there some better way to solve my problem? 有解决我问题的更好方法吗?

In your case you don't need ListView at all. 在您的情况下,您根本不需要ListView。 As the reference says http://developer.android.com/reference/android/widget/ListView.html ListView can only show items in vertically scrolling list. 如参考文献所述, http//developer.android.com/reference/android/widget/ListView.html ListView只能在垂直滚动列表中显示项目。 Just create relative layout and add child views manually. 只需创建相对布局并手动添加子视图即可。

Create XML file something like this for ListView items: ListView项目创建类似以下的XML文件:

listview_item.xml listview_item.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" >

  <TextView 
     android:id="@+id/text1"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />
</RelativeLayout>

Then in ListView_Adapter class do: 然后在ListView_Adapter类中执行以下操作:

public class ListView_Adapter extends ArrayAdapter<String>
{
    public ListView_Adapter(Context c)
    {
        super(c, R.layout.listview_item);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
  //
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM