自定义视图不填充父级

Ben*_*ams 2 xml layout android

我有这样的自定义视图

public class ButtonBar extends HorizontalScrollView
{
  public View mButtonRows;

  public ButtonBar(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mButtonRows = inflater.inflate(R.layout.toolbar, null);

    // button click handling code goes here

    addView(mButtonRows);
  }
}
Run Code Online (Sandbox Code Playgroud)

这包含在我的主要xml中

<com.example.ButtonBar
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:layout_alignParentBottom="true"
  android:layout_below="@+id/pagecontent" />
Run Code Online (Sandbox Code Playgroud)

并膨胀像这样的xml文件:

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ButtonsRow" 
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <Button 
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="0.3"
    android:text="button1"
  />
  <Button 
    android:id="@+id/button2"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="0.3"
    android:text="button2"
  />
  <Button 
    android:id="@+id/button3"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="0.3"
    android:text="button3"
  />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

(它目前只有三个按钮,但在更高版本中将需要更多按钮,因此Horizo​​ntalScrollView.)

查看hierarchyviewer,自定义视图看起来似乎是屏幕宽,但LinearLayout只有它所包含的按钮宽(当前按钮大小约为屏幕的2/3),尽管设置了fill_parent宽度; 按钮不伸展.如果我将LinearLayout的背景设置为@android:drawable/bottom_bar(这是屏幕宽度的png),则按钮会正确调整大小; 我意识到我可以通过创建自己的图像来做同样的事情来匹配,但我宁愿在没有可能的情况下做到这一点.

我究竟做错了什么?


ETA:如果我将Horizo​​ntalScollView更改为ScrollView,它可以正常工作.HSV不允许他们的孩子"fill_parent"吗?


ETA2:android:fillViewport="true"在主xml中设置修复它!

Ben*_*ams 6

在主xml中设置android:fillViewport ="true"修复它!