android - 布局问题 : SliderDrawer doesn't fill parent width

标签 android android-layout

我在 Android 中使用 SliderDrawer。在抽屉里,我有一个布局,其中包含一个膨胀的布局。这是最终结果:

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);

最佳答案

与其膨胀您的内容布局然后将其添加为嵌套线性布局,不如尝试将您想要的布局直接包含在 media_player_container 布局中。从简单开始(也许只是让它的内容是一个具有纯色背景且没有填充的 View )并从那里开始,直到您找出触发问题的原因。

您可能还需要考虑替换这些行:

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)

通过将 View 直接扩展到容器中,您允许 View 系统正确管理您的布局属性(技术上适用于容器,而不是您定义它们的 View )。没有它们,在您将它们添加为 subview 之前,您的膨胀 View 中定义的布局属性将被丢弃(因为它不知道您将它们膨胀到哪种类型的布局)。

关于android - 布局问题 : SliderDrawer doesn't fill parent width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4853090/

相关文章:

java - 突出显示 ListView 选择

java - 如何重命名内部存储中的文件?

java - 如何使用 RemoteViews 更改小部件背景?

java - 带有 float 按钮的 ScrollView

android - 如何通过代码编程获取设备的 IMEI/ESN 号码 但是在 android > 6

java - 我想在一项 Activity 中显示两个自定义对话

android - Android 中的 3D 立方体过渡

Android 2.3.3 ListView停止垂直滚动

android - 结合 Android 平台风格

java - 如何更改 Android 中的菜单项大小?