【问题标题】:Getting unwanted margin获得不需要的保证金
【发布时间】:2016-11-18 22:12:45
【问题描述】:

我有以下 XML:-

对于列表视图:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="1">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/state_listView"
        android:layout_weight="0.8"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="40dp"
        android:fastScrollEnabled="true"
        android:layout_toStartOf="@+id/side_index" />
    <LinearLayout
        android:id="@+id/side_index"
        android:layout_width="50dp"
        android:layout_marginTop="40dp"
        android:layout_height="match_parent"
        android:background="#c3c3c3"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_weight="0.2"
        android:layout_alignParentRight="true">
    </LinearLayout>
</RelativeLayout>

查看列表视图中的每个项目:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:background="@drawable/gradient">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">
        <ImageView
            android:layout_width="55dp"
            android:layout_height="49dp"
            android:id="@+id/legislator_image"
            android:layout_weight="0.19"
            android:layout_alignParentLeft="true"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="80dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:text="Medium Text"
                android:id="@+id/firstname"
                android:layout_marginTop="3dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/details"
                android:layout_marginTop="3dp" />

        </LinearLayout>

        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="3dp"
            android:layout_alignParentRight="true"
            android:id="@+id/getDetails"/>
    </RelativeLayout>
</LinearLayout>

对于侧面索引栏:-

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/side_list_item"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:textSize="13sp" />

我没有在任何地方设置边距,但在列表视图和侧边栏附近有一个边距。我希望列表视图和侧边栏占据整个屏幕而没有任何边距。我已附上一张图片供您参考。

【问题讨论】:

  • 您发布的 XML 是否从片段中显示?如果是这样,我怀疑父 Activity 的 XML 容器可能有一些额外的填充。尝试在包含 Activity 的内容中查找对填充的任何引用。

标签: android android-layout listview margins


【解决方案1】:

我不确定它是否会解决您的问题,也许还有更多问题,但您的代码肯定是错误的。

1) 当你使用layout_weight时,你应该设置layout_width="0dp"(如果父布局是垂直的,则分别设置layout_height="0dp"),layout_width="0dp"会导致,该属性将被忽略,所以layout_width可以正常工作.

2) 第二件事。当您使用layout_weight 时,您应该在父布局中将layout_width 设置为某个确定的值,在您的情况下设置为match_parent 而不是wrap_content

There is good explanation

【讨论】:

    【解决方案2】:

    将相对布局(包含 ListView 和 LinearLayout 的那个)宽度参数更改为:

    android:layout_width="match_parent"
    

    使用android:layout_width="wrap_content",RelativeLayout 只会随着其内容将其侧向推而增长,所以边距不存在,因为您定义了它,这是因为 Layout 没有足够的宽度到达边界

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多