我有一个垂直布局,在底部我有一个HorizontalScrollView
。在ScrollView
里面,我想显示四个容器,这些容器应该具有完全相同的宽度。
这些容器中的每一个都加载了一个片段,该片段使宽度和高度match_parent的布局膨胀。在发出一些网络请求之后,我只填充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>
答案 0 :(得分:0)
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)); } }