相对布局的宽度和高度设置为fill_parent无效

3
这是我的布局,它是一个信息窗口,在需要向用户显示新信息时显示。
布局应该占据整个屏幕,并带有一些透明的背景颜色,以及一个小的文本布局:
<?xml version="1.0" encoding="utf-8"?>
<!-- info fragment - used by the fragment to provide info on a specific fragment -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_dark_green" >

    //this is the only layout i see in the result. not it's parent's layout...
    <RelativeLayout
        android:id="@+id/fragment_info_relativelayout_info"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/background_info_window"
        android:clickable="true" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:layout_margin="5dp" >

            <!-- The info textView -->

            <com.coapps.pico.NillanTextView
                android:id="@+id/fragment_info_textview_info"
                style="@style/text_shadow_black"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:gravity="left"
                android:text="Some Text "
                android:textColor="@android:color/white"
                app:isBold="true" />
        </ScrollView>
        <!-- Tap to close -->

        <com.coapps.pico.NillanTextView
            android:id="@+id/fragment_info_textview_tap_to_close"
            style="@style/text_shadow_black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:paddingRight="5dp"
            android:text="@string/button_tap_to_close"
            android:textColor="@android:color/white"
            app:isBold="true" />
    </RelativeLayout>

</RelativeLayout>

我正在将此布局添加到另一个ViewGroup中,代码如下:
/**
 * Show the info window
 */
public void show()
{
    //if the window is not visible
    if (!isVisible())
        //add window's view
        rootView.addView(infoWindowView);
}

这段代码将信息窗口添加到其根视图容器的最后一个索引中。
结果中我只看到包含文本的第二个RelativeLayout,但我没有看到透明的父级根布局...
有任何想法为什么会这样?
更新: 这是我的容器:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_basic_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</RelativeLayout>
3个回答

0

我也遇到了一个问题,就是相对布局中的一个视图依赖于相对布局的大小。 我通过更新内部视图的大小来解决这个问题。

private void update viewSize()
if(rootContainer == null || rootContainer.getLayoutParams() == null)
return;
rootContainer.measure(View.MeasureSpec.makeMeasureSpec(ScalingUtil.getScreenSize().getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));

    RelativeLayout.LayoutParams oldLayoutParams = (RelativeLayout.LayoutParams) rootContainer.findViewById(R.id.deals_card_center_bg).getLayoutParams();
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rootContainer.getMeasuredWidth(), rootContainer.getMeasuredHeight());
    layoutParams.setMargins(oldLayoutParams.leftMargin, oldLayoutParams.topMargin, oldLayoutParams.rightMargin, oldLayoutParams.bottomMargin);

    rootContainer.findViewById(R.id.deals_card_center_bg).setLayoutParams(layoutParams);

0

如何避免父级元素扩展

不要在子元素上使用match_parent,而是将其与决定高度的兄弟元素对齐。

<RelativeLayout ...>

    <View
        android:id="@+id/element_that_should_match_its_parents_height"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/element_that_dictates_height" />

    <View
        android:id="@+id/element_that_dictates_height"

        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

对我有用,希望能帮到你。第二个View通常是一些ViewGroup,其高度会有所不同。


-1
尝试更改
<RelativeLayout
        android:id="@+id/fragment_info_relativelayout_info"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/background_info_window"
        android:clickable="true" >

<RelativeLayout
        android:id="@+id/fragment_info_relativelayout_info"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/background_info_window"
        android:clickable="true" >

不过,为什么需要嵌套的RelativeLayout呢?

我知道你是如何添加视图的,所以也试试这个:

infoWindowView.setLayoutParams(
       new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
rootView.addView(infoWindowView);

由于我需要第一个(根)布局填满整个屏幕并使用透明颜色来将焦点转移到第二个相对布局。 - Asaf Nevo
它似乎不起作用...即使如此,我真的不明白为什么它应该起作用?我的根布局宽度和高度的fill_parent为什么不起作用?这以前从未发生过。 - Asaf Nevo
这是一个复合自定义视图的内容吗?容器是什么? - Seraphim's
我已经添加了容器... 您所说的复合自定义视图是什么意思? - Asaf Nevo
没有自定义视图..唯一的问题是我的NillanTextView已经设置了TypeFace字体,因为我无法在XML中设置它。 - Asaf Nevo
好的,请按照上述说明设置LayoutParams。 - Seraphim's

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接