Проблема макета: SliderDrawer не заполняет родительскую ширину - PullRequest
3 голосов
/ 31 января 2011

Я использую SliderDrawer в Android. Внутри ящика есть макет, который содержит раздутый макет. Вот конечный результат:

enter image description here

Обратите внимание, что содержимое ящика (серого цвета) не занимает всю ширину родительского элемента (черная область).

Вот код ящика (main_layout.xml):

[ОБНОВЛЕНИЕ 2, добавлен весь код:]

<?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="fill_parent">
  <LinearLayout android:id="@+id/banner" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:gravity="center" android:padding="3px">
    <ImageView android:src="@drawable/title"
      android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <TextView android:layout_width="wrap_content" android:text="@string/title"
      android:layout_height="wrap_content" android:textSize="18sp"
      android:gravity="center_vertical" android:paddingLeft="4px"
      android:textStyle="bold" />
  </LinearLayout>
  <LinearLayout android:layout_below="@id/banner" android:id="@+id/middle" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <View android:layout_width="fill_parent" android:layout_height="1dip"
      android:background="#FF555555" />
    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:padding="3px"
      android:layout_weight="1" />
    <TextView android:id="@android:id/empty" android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:text="@string/no_items"
      android:padding="3px" android:layout_weight="1" />
  </LinearLayout>
  <LinearLayout android:id="@+id/LinearLayout01" android:layout_alignParentBottom="true"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="vertical" android:gravity="center_horizontal|bottom">
    <SlidingDrawer android:id="@+id/drawer" android:background="#22222222" 
      android:layout_height="110dip" android:handle="@+id/handle"
      android:content="@+id/media_player_container" android:layout_width="fill_parent">
      <ImageView android:id="@+id/handle" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:src="@drawable/tray" android:scaleType="centerCrop"/>
      <LinearLayout android:id="@+id/media_player_container"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="bottom|center_horizontal" />
    </SlidingDrawer>
  </LinearLayout>
</RelativeLayout>

Вот код раздутой области (media_player_container.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"   android:gravity="bottom|center_horizontal" android:orientation="vertical"  android:background="#FF555555" android:padding="3px">
  <SeekBar android:id="@+id/progress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="3px" android:progress="1000" />
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"  android:orientation="horizontal">
    <ImageButton android:id="@+id/speakerhandset" android:src="@drawable/handset" android:layout_width="60dip" android:layout_height="wrap_content" android:scaleType="center"/>
    <ImageButton android:id="@+id/rewind" android:src="@android:drawable/ic_media_rew" android:layout_width="60dip" android:paddingRight="3px" android:layout_height="wrap_content" android:scaleType="center"/>
    <ImageButton android:id="@+id/play" android:src="@android:drawable/ic_media_play" android:layout_width="60dip" android:paddingRight="3px" android:layout_height="wrap_content" android:scaleType="center"/>
    <ImageButton android:id="@+id/pause" android:src="@android:drawable/ic_media_pause" android:layout_width="60dip" android:paddingRight="3px" android:layout_height="wrap_content" android:visibility="gone"  android:scaleType="center"/>
    <ImageButton android:id="@+id/fastforward" android:src="@android:drawable/ic_media_ff" android:layout_width="60dip" android:paddingRight="3px" android:layout_height="wrap_content" android:scaleType="center"/>
  </LinearLayout>
</LinearLayout>

Можете ли вы определить, что я делаю неправильно?

[Обновление 1] Вот код (внутри onCreate ()), где я раздуваю макет:

LinearLayout container = (LinearLayout) this.findViewById(R.id.media_player_container);

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.media_player, null, false);
container.addView(layout);

Ответы [ 4 ]

6 голосов
/ 25 марта 2011

Вместо того, чтобы раздувать ваш макет контента и затем добавлять его как вложенный линейный макет, попробуйте просто включить нужный макет непосредственно в макет media_player_container. Начните с простого (возможно, просто включите его в представление с твердым фоном и без отступов) и продолжайте свой путь оттуда, пока не сможете выяснить, что вызывает проблему.

Вы также можете рассмотреть возможность замены этих строк:

LinearLayout container = (LinearLayout) this.findViewById(R.id.media_player_container);

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.media_player, null, false);
container.addView(layout);

с:

View.inflate(this, R.layout.media_player, 
        (ViewGroup) findViewById(R.id.media_player_container)

Накачивая представление непосредственно в контейнер, вы позволяете системе представления правильно управлять атрибутами макета (которые технически применимы к контейнеру, а не к виду, для которого вы их определяете). Без них атрибуты макета, определенные в ваших завышенных представлениях, отбрасываются до того, как вы добавите их как дочерний элемент (поскольку он не может знать, к какому типу макета вы их надуваете).

2 голосов
/ 29 марта 2011

Вам нужно дать LayoutParams при добавлении представления.

container.addView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
1 голос
/ 23 марта 2011
<SlidingDrawer android:id="@+id/drawer" android:padding="10dip" android:background="#22222222" android:layout_height="110dip" android:handle="@+id/handle" android:content="@+id/media_player_container" android:layout_width="fill_parent">

Обратите внимание на android: padding = "10dip".Ширина выдвижного ящика заполняет родительский элемент, но у вас есть 10 точек на всех четырех сторонах.Если вы хотите, чтобы отступы сверху и снизу оставались, выполните:

android:paddingTop="10dip"
android:paddingBottom="10dip"
1 голос
/ 23 марта 2011

Вы указываете атрибут layoutHeight для выдвижного ящика.Установите высоту макета в FillParent, и это должно сделать это ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...