RelativeLayout的保证金里面没有工作的ListView [英] RelativeLayout margin not working inside listView

查看:159
本文介绍了RelativeLayout的保证金里面没有工作的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我想要做到的是不能正常工作

相对布局code:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#649760
            机器人:layout_marginRight =30dp>    < ImageView的
        机器人:layout_width =200dp
        机器人:layout_height =50dp
        机器人:背景=#000000/>< / RelativeLayout的>

它的外观在Android工作室preVIEW:

注意右边白色的绿色。

我还以去除分频器和控制背景色添加list_view.xml

 <?XML版本=1.0编码=UTF-8&GT?;
 < ListView控件的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      机器人:layout_width =match_parent
      机器人:layout_height =WRAP_CONTENT
      机器人:dividerHeight =0dp
      机器人:分=@空
      机器人:背景=#FFFFFF>
 < /&的ListView GT;

但什么我得到(从我的手机屏幕捕获)是绿色捕获所有剩余的宽度。

BTW我已经通过给填充解决它list_view.xml,但因为我有在列表中多个视图,而不是所有的人都应该得到这样的保证金,我可以不使用这样的解决方案

任何帮助将大大AP preciated,并将与竖起大拇指给予奖励( - :


解决方案

一个ListView使用 AbsListView.LayoutParams ,当你检查出文档的行:
http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html

你可以看到,他们是不提供任何保证金。

所以,解决办法是增加另一个查看在你的细胞上面,这应该工作:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =match_parent
              机器人:layout_height =WRAP_CONTENT>
    < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#649760
            机器人:layout_marginRight =30dp>        < ImageView的
                机器人:layout_width =200dp
                机器人:layout_height =50dp
                机器人:背景=#000000/>    < / RelativeLayout的>
< / LinearLayout中>

这是其他的解决方法,在你的rootView右侧添加视图

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=#649760>    <查看
            机器人:layout_width =30dp
            机器人:layout_height =50dp
            机器人:layout_alignParentRight =真
            机器人:背景=@色/白/>    < ImageView的
            机器人:layout_width =200dp
            机器人:layout_height =50dp
            机器人:背景=#000000/>< / RelativeLayout的>

我知道这是不是最好的性能,但它是我发现的唯一的解决方法。

I'm wondering why what i'm trying to accomplish isn't working

Relative layout code:

<?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="wrap_content"
            android:background="#649760" 
            android:layout_marginRight="30dp">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="#000000"/>

</RelativeLayout>

How it looks on android studio preview:

notice the white right to the green.

I've also added a list_view.xml in order to remove the divider and control the background color

 <?xml version="1.0" encoding="utf-8"?>
 <ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:dividerHeight="0dp"
      android:divider="@null"
      android:background="#ffffff">
 </ListView>

but what i'm getting (screen capture from my mobile) is that the green captures all the remaining width.

btw i've solved it by giving a padding to list_view.xml but i cannot use such a solution because i have multiple views in the list and not all of them should get this kind of margin

Any help will be greatly appreciated, and will be rewarded with thumbs up (-:

解决方案

The rows of a ListView use AbsListView.LayoutParams, when you check out the doc : http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html

you can notice that they aren't any margin available.

So the workaround is to add another View on top of your cells, this should work :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#649760"
            android:layout_marginRight="30dp">

        <ImageView
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:background="#000000"/>

    </RelativeLayout>
</LinearLayout>

An other workaround, add a View at the right of your "rootView" :

<?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="wrap_content"
        android:background="#649760">

    <View
            android:layout_width="30dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@color/white"/>

    <ImageView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#000000"/>

</RelativeLayout>

I know it's not the best for performance, but it's the only workaround I found.

这篇关于RelativeLayout的保证金里面没有工作的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆