简体   繁体   English

Android Horizo​​ntalScrollView内容拉伸

[英]Android HorizontalScrollView content Stretch

I have a HorizontalScrollView with some buttons, I am trying to stretch these buttons to fill the whole width of the HorizontalScrollView so that they can appear organized and equally spaced between each others. 我有一个带有某些按钮的Horizo​​ntalScrollView,我试图拉伸这些按钮以填充Horizo​​ntalScrollView的整个宽度,以便它们可以看起来井井有条,彼此之间等距。 Here is the Image which I have. 这是我的图像。 Horizo​​ntalScrollView内容未拉伸

I want these 4 buttons to be stretched over the whole width! 我希望这4个按钮可以在整个宽度上伸展! here is my code 这是我的代码

    <HorizontalScrollView
    android:id="@+id/items_HorizontalBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/vf"
    android:background="#80000000"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:layout_width="90dp"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/rss"
            android:text="Light"
            android:textColor="@color/green_color"
            android:textSize="20sp" />

        <Button
            android:layout_width="90dp"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/rss"
            android:text="Door"
            android:textColor="@color/green_color"
            android:textSize="20sp" />

        <Button
            android:layout_width="90dp"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/rss"
            android:text="Alarms"
            android:textColor="@color/green_color"
            android:textSize="20sp" />

        <Button
            android:layout_width="90dp"
            android:layout_height="fill_parent"
            android:layout_margin="5dp"
            android:background="@android:color/transparent"
            android:drawableTop="@drawable/rss"
            android:text="Window"
            android:textColor="@color/green_color"
            android:textSize="20sp" />
    </LinearLayout>
</HorizontalScrollView>

Instead of messing with LinearLayout you should follow the correct solution that is setting the HorizontalScrollView (or Vertical) to FillViewPort . 您应该遵循正确的解决方案,即将HorizontalScrollView (或Vertical)设置为FillViewPort而不是与LinearLayout FillViewPort

XML: XML:

android:fillViewport="true"

Programmatically: 编程方式:

hsv.setFillViewport(true);

try making the container a horizontal scroll view. 尝试使容器成为水平滚动视图。 After that add in a table layout, and in each row of the table add in a horizontal linear layer. 之后,添加表格布局,并在表格的每一行中添加水平线性层。 what will now happen instead, is that the scroll view will be stretched to fit the button size you set, and should scroll w/o you having to program a thing, and you should have effectively created a grid. 现在将发生的事情是,滚动视图将被拉伸以适合您设置的按钮大小,并且在无需编写任何东西的情况下应该滚动,并且您应该已经有效地创建了网格。 try something similar to this: 试试类似的东西:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sc1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" >

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >



            <Button
                android:id="@+id/button1"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:text="Button" />



            <Button
                android:id="@+id/button2"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:text="Button" />



            <Button
                android:id="@+id/button3"
                android:layout_width="230dp"
                android:layout_height="wrap_content"
                android:text="Button" />



            <Button
                android:id="@+id/button4"
                android:layout_width="233dp"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

Also perhaps you could wrap the horizontal scroll view in a vertical scroll view then you can scroll up/down, left/right and do as you need. 同样,也许您可​​以将水平滚动视图包装在垂直滚动视图中,然后可以向上/向下,向左/向右滚动并根据需要进行操作。

For equal distributions, set the weightSum value of the parent, and assign layout_weight to its children. 对于相等的分布,请设置父级的weightSum值,然后将layout_weight分配给其子级。

For 4 equal sized buttons, add android:weightSum="1" to the LinearLayout. 对于4个大小相等的按钮,将android:weightSum="1"到LinearLayout。 For each of the buttons set android:layout_width="0dp" , then add android:layout_weight="0.25" . 为每个按钮设置android:layout_width="0dp" ,然后添加android:layout_weight="0.25"

This is what occurs in the code but depending on the View, the "Distribute Weights Evenly" button in the Eclipse GUI can also help. 这就是代码中发生的情况,但是根据视图,Eclipse GUI中的“平均分配权重”按钮也可以提供帮助。

However, HorizontalScrollView can only host one direct child, I wonder about the structure of this layout... 但是, Horizo​​ntalScrollView只能容纳一个直子,我想知道这种布局的结构...

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

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