android – 布局问题:SliderDrawer不填充父宽度

我在Android中使用SliderDrawer.在抽屉里面我有一个包含充气布局的布局.这是最终结果:

请注意抽屉的内容(灰色)如何不占用父级的整个宽度(黑色区域).

这是抽屉代码(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);

解决方法:

而不是膨胀您的内容布局,然后将其添加为嵌套的线性布局,请尝试直接在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)

通过将视图直接膨胀到容器中,您可以允许视图系统正确管理布局属性(这在技术上适用于容器,而不是您在其上定义的视图).如果没有它们,膨胀视图中定义的布局属性将在您作为子项添加之前被丢弃(因为它无法知道您将它们扩展到哪种类型的布局).

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...