Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在水平滚动视图中以相同的宽度进行布局_Android - Fatal编程技术网

Android在水平滚动视图中以相同的宽度进行布局

Android在水平滚动视图中以相同的宽度进行布局,android,Android,我有一个垂直布局,底部有一个水平滚动视图。在这个ScrollView中,我想显示四个容器,这些容器的宽度应该完全相同。 这些容器中的每一个都装载有一个碎片,该碎片可以使布局的宽度和高度与父级匹配。在发出一些网络请求后,我只填充滚动视图中的容器,因此在加载所有数据后,我可以进行以下操作: first_small_container_data -> width = 100px second_small_container_data -> width = 200px th

我有一个垂直布局,底部有一个
水平滚动视图
。在这个
ScrollView
中,我想显示四个容器,这些容器的宽度应该完全相同。 这些容器中的每一个都装载有一个碎片,该碎片可以使布局的宽度和高度与父级匹配。在发出一些网络请求后,我只填充
滚动视图
中的容器,因此在加载所有数据后,我可以进行以下操作:

first_small_container_data      -> width = 100px
second_small_container_data     -> width = 200px
third_small_container_data      -> width = 230px
fourth_small_container_data     -> width = 112px
我希望在加载数据后,
HorizontalScrollView
中的所有容器都具有最大宽度,在本例中为
230px

我试图通过以下布局实现这一点,但没有成功

有没有办法只用XML解决这个问题?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/big_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:id="@+id/first_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" />

    <LinearLayout
        android:id="@+id/second_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" />

    <HorizontalScrollView
        android:id="@+id/small_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/small_container_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="4">

            <LinearLayout
                android:id="@+id/first_small_container_data"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:orientation="horizontal"
                android:layout_weight="1" />

            <LinearLayout
                android:id="@+id/second_small_container_data"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:orientation="horizontal"
                android:layout_weight="1" />

            <LinearLayout
                android:id="@+id/third_small_container_data"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:orientation="horizontal"
                android:layout_weight="1" />

            <LinearLayout
                android:id="@+id/fourth_small_container_data"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="5dp"
                android:orientation="horizontal"
                android:layout_weight="1" />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

公共类测试活动扩展了活动{ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_hz); /*加载数据*/ //数据加载后 //将容器设置为最大宽度:230px; int maxWidth=230; LinearLayout firstSmallContainer=(LinearLayout)findViewById(R.id.first\u small\u container\u数据); LinearLayout secondSmallContainer=(LinearLayout)findViewById(R.id.second\u small\u container\u数据); LinearLayout ThirdsAllContainer=(LinearLayout)findViewById(R.id.third\u small\u container\u数据); LinearLayout fourthSmallContainer=(LinearLayout)findViewById(R.id.fourth\u small\u container\u数据); firstSmallContainer.setLayoutParams(新的LinearLayout.LayoutParams(maxWidth,20)); secondSmallContainer.setLayoutParams(新的LinearLayout.LayoutParams(maxWidth,20)); ThirdsAllContainer.setLayoutParams(新的LinearLayout.LayoutParams(maxWidth,20)); 第四个SmallContainer.setLayoutParams(新的LinearLayout.LayoutParams(maxWidth,20)); } }
我想补充一点,你可以用
LinearLayout.LayoutParams.WRAP\u CONTENT
抱歉来替换那些20岁的孩子。编辑我的问题是因为我只想知道是否可以只使用XML public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hz); /* Load Data */ // After data is loaded // Set containers to maximum width: 230px; int maxWidth = 230; LinearLayout firstSmallContainer = (LinearLayout) findViewById(R.id.first_small_container_data); LinearLayout secondSmallContainer = (LinearLayout) findViewById(R.id.second_small_container_data); LinearLayout thirdSmallContainer = (LinearLayout) findViewById(R.id.third_small_container_data); LinearLayout fourthSmallContainer = (LinearLayout) findViewById(R.id.fourth_small_container_data); firstSmallContainer.setLayoutParams(new LinearLayout.LayoutParams( maxWidth, 20)); secondSmallContainer.setLayoutParams(new LinearLayout.LayoutParams( maxWidth, 20)); thirdSmallContainer.setLayoutParams(new LinearLayout.LayoutParams( maxWidth, 20)); fourthSmallContainer.setLayoutParams(new LinearLayout.LayoutParams( maxWidth, 20)); } }