RelativeLayout宽度和高度fill_parent不起作用

时间:2013-10-17 15:40:36

标签: android view relativelayout

这是我的布局,它是一个信息窗口,显示了向用户显示新信息的位置。

布局应该使整个屏幕具有一些透明的背景颜色和小文本布局:

<?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 :(得分: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);

答案 1 :(得分:0)

如何不让父展开

而不是孩子的CascadeType.PERSIST,将其与指示身高的兄弟姐妹对齐。

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,其高度不同。

答案 2 :(得分:-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);