简体   繁体   English

如何使Horizo​​ntalScrollView的孩子的孩子具有屏幕的宽度?

[英]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. 所以我有一个LinearLayout,它的layout_width设置为“ match_parent”,我们称它为A。

Then there's a HorizontalScrollView within that, let's call that B. 然后是其中的Horizo​​ntalScrollView,我们称其为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. 然后里面有一个RelativeLayout,我使用它是因为该规则只允许1个直接子int滚动视图,所以我们称之为C。

Then within that I have multiple RelativeLayouts that I want to make the same width as A, alongside each other. 然后在其中有多个RelativeLayouts,我想使它们与A的宽度相同,并且彼此相邻。 Please help me with how I can accomplish this! 请帮助我如何实现这一目标!

Here is the XML code: 这是XML代码:

<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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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