如何让Horizo​​ntalScrollView的孩子拥有屏幕的宽度?

时间:2016-11-27 05:56:44

标签: java android xml

所以我有一个LinearLayout,它将layout_width设置为" match_parent",让我们调用A.

然后在那里有一个Horizo​​ntalScrollView,让我们称之为B.

然后在那里有一个RelativeLayout,我正在使用因为规则只允许1个直接子节点滚动视图,让我们调用那个C.

然后在其中我有多个RelativeLayouts,我希望与A一起制作相同的宽度。请帮我解决这个问题!

以下是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">

1 个答案:

答案 0 :(得分:0)

使用以下实用程序方法查找屏幕宽度。

static void Main(string[] args)
        {
            string res = null;
            string toggle = null;
            res += "<div style=\"display: none;\">This is the content that is dynamically being collapsed.</div>";
            toggle += "<html><head>";
            toggle += "<script type=\"text/javascript\"></script><script> function toggle2(showDetails) {var ele =document.getElementById(showDetails);if(ele.style.display == \"block\") {ele.style.display = \"none\";}else {ele.style.display = \"block\";}}</script>";
            toggle += "</head>";
            toggle += "<body>";
            toggle += "<a href=\"javascript:toggle2('"+res+"')\">collapse</a>";
            toggle += "</body>";
            toggle += "</html>";

            FileStream log = new FileStream(@"E:\report2.html", FileMode.Create);
            using (StreamWriter w = new StreamWriter(log, Encoding.UTF8))
            {
                w.WriteLine(toggle);
            }
        }

然后将该宽度分配给您的孩子RelativeLayout

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