[英]Getting unwanted margin
I have the following XML:- 我有以下XML:
For the list view:- 对于列表视图:-
<?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>
Foe each item in the list view:- 仇视列表视图中的每个项目:-
<?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>
For the side index bar :- 对于侧面索引栏:-
<?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" />
I have not set the margin anywhere but there is a margin on the listview and near the sidebar. 我没有在任何地方设置边距,但是在列表视图和边栏附近有一个边距。 I want the listview and the sidebar to take up the whole screen without any margin.
我希望listview和侧边栏占据整个屏幕,而没有任何边距。 I have attached an image for your reference.
我已附上一张图片供您参考。
I am not sure, that it will fix your problem, maybe there are something more, but it is definitely wrong in your code. 我不确定,它是否可以解决您的问题,也许还有其他问题,但这在您的代码中绝对是错误的。
1) When you use layout_weight
, you should set layout_width="0dp"
(respectively layout_height="0dp"
if parent layout is vertical), layout_width="0dp"
will cause, that this attribute will be ignored, so layout_width
can work properly. 1)使用
layout_weight
,应设置layout_width="0dp"
(如果父布局是垂直的,则分别设置layout_height="0dp"
), layout_width="0dp"
将导致该属性被忽略,因此layout_width
可以正常工作。
2) And second thing. 2)第二件事。 When you use
layout_weight
, you should set layout_width
in parent layout to some definite value, in your case to match_parent
instead of wrap_content
使用
layout_weight
,应将父布局中的layout_width
设置为某个确定的值,在这种情况下,应设置为match_parent
而不是wrap_content
Change you relative layout (the one containing the ListView and the LinearLayout) width param to: 将您的相对布局(包含ListView和LinearLayout的一个)的宽度参数更改为:
android:layout_width="match_parent"
With the android:layout_width="wrap_content"
the RelativeLayout only grows as much as its contents push it sideways, so the margin is not there because you defined it, its because the Layout does not get enough width to reach the borders 使用
android:layout_width="wrap_content"
,RelativeLayout只会随着其内容向侧面推动而增长,因此没有边距,因为您定义了它,这是因为Layout的宽度不足以到达边界
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.