简体   繁体   中英

How can I make a HorizontalScrollView's child's child have the width of the screen?

So I've got a LinearLayout, that has the layout_width set to "match_parent", let's call that A.

Then there's a HorizontalScrollView within that, let's call that B.

Then there's a RelativeLayout within that, that I am using because of the rule that only allows 1 direct child int he scroll view, lets call that C.

Then within that I have multiple RelativeLayouts that I want to make the same width as A, alongside each other. Please help me with how I can accomplish this!

Here is the XML code:

<LinearLayout 
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >


<HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_weight="9">

    // Used because ScrollView can have only 1 child.
    <RelativeLayout
        android:id="@+id/directChild"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        // This is the RelativeLayout that I want to fill the screen width with.
        <RelativeLayout
            android:id="@+id/howToMakeScreenWidth"
            android:layout_width="385dp"
            android:layout_height="match_parent">

Find width of screen using below utility method.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;

then assign that width to your child RelativeLayout

RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(
                width, ViewGroup.LayoutParams.WRAP_CONTENT);
yourRelativeLayout.setLayoutParams(relParams);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM