Маржа на элементах ListView в android
Я пытаюсь (зря) добавлять поля в элементы ListView. Я попытался добавить значения маржи к моему RelativeLayout ниже, но независимо от того, что я делаю все, что я, кажется, получаю, это линия 1px между каждым элементом.
Мне бы хотелось, чтобы на каждом элементе были закругленные углы, черная рамка 1px и поле на 3-5 пикселей слева, сверху и справа, но сейчас я соглашусь только на маржу вокруг каждого элемента:-)
Как достичь моих целей? Только край на данный момент...; -)
Вот что у меня есть:
ОБНОВЛЕНИЕ: Я обновил xml, удалив основной макет и макет фрагмента. Я также обновил макет элемента ListView до того, что у меня есть сейчас, что ближе к тому, что я хочу, но все же не идеальному. Скриншот добавлен также
список элементов списка xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/matchMargin"
android:paddingRight="@dimen/matchMargin"
android:paddingTop="@dimen/matchMargin" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cfcfcfcf" >
<include
android:id="@+id/matchKampstart"
layout="@layout/kampstart_layout" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/matchKampstart"
android:layout_marginTop="@dimen/belowKampstartMargin" >
<ImageView
android:id="@+id/tournamentImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:adjustViewBounds="true"
android:contentDescription="@string/tournamentImageViewContentDescription"
android:gravity="left"
android:src="@drawable/sofabold_launcher" />
<ImageView
android:id="@+id/homeTeamImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:adjustViewBounds="true"
android:contentDescription="@string/homeTeamImageViewContentDescription"
android:src="@drawable/sofabold_launcher" />
<TextView
android:id="@+id/homeTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:text="@string/home"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/dash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:gravity="center"
android:text="@string/dash"
android:textSize="12sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/awayTeamImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:adjustViewBounds="true"
android:contentDescription="@string/awayTeamImageViewContentDescription"
android:src="@drawable/sofabold_launcher" />
<TextView
android:id="@+id/awayTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="@string/away"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/matchKampstart"
android:layout_marginTop="@dimen/belowKampstartMargin" >
<ImageView
android:id="@+id/tvChannelImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:contentDescription="@string/tvChannelImageViewContentDescription"
android:gravity="right"
android:src="@drawable/sofabold_launcher" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Это дает мне следующее, где вы заметите очень маленькую строку справа и слева для каждого элемента. Что я также хотел бы избавиться.
![enter image description here]()
Ответы
Ответ 1
Я не очень хорош с макетами, но в прошлом я заметил, что строки ListView часто игнорируют LayoutParams. Я понятия не имею, где это происходит или можно ли переопределить, я знаю, что вы можете легко обойти его, добавив еще один макет:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#990000ff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#9900ff00"
android:paddingLeft="10dp" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99ff0000" />
</LinearLayout>
</LinearLayout>
Обычно макеты, у которых есть только один ребенок, могут быть удалены, но, как вы видите, это служит цели:
![Screen Shot]()
Внешний внешний вид - синий, TextView - красный, а зеленый - дополнительный макет, который позволяет добавить дополнительный интервал. Обратите внимание на разницу между отступом (зеленый слева) и краем (зеленый не зеленый). Вы четко указали, что хотите использовать поля (android:layout_margin
), но ваш код явно использует отступы (android:padding
), поэтому я включил оба.
Ответ 2
Немного поздно, увидев это, но просто добавьте следующие строки в ваш элемент xml ListView.
android:divider="#00000000"
android:dividerHeight="10dp"
Ответ 3
См. ответ здесь, почему. Короче говоря, ребенок запрашивает родителя, а в строке списка используется AbsListView.LayoutParams
, который не включает поля.
Почему маржа LinearLayout игнорируется при использовании в качестве представления строки ListView
Ответ 4
В вашем адаптере уловите свой относительный макет в getView(), затем дайте параметры макета,
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom
YourRelativeLayoutInAdapter.setLayoutParams(params);
Ответ 5
Добавьте layout_width
в свой ListView
и поместите его в центр или переместите его на margin
.
(Может быть, android:layout_centerHorizontal="true"
.)
Он отлично работал у меня.
Ответ 6
Я предполагаю, что у вас есть ListView
, определенный в XML файле где-то, если это так, вы можете добавить к нему несколько padding
, чтобы между краем экрана и ListView было некоторое пространство.
Пример:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"/>