我希望有5个图像视图,一个彼此相邻(水平),每个图像视图的宽度应为屏幕的1/5。我没有测量和设置活动内部的宽度,而是采用线性布局来实现结果:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<com.davidbalas.brawlstarsinfo.CustomTextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="@android:color/white"
android:textSize="28sp"
android:textStyle="bold"
app:fontName="supercell.ttf" />
<com.davidbalas.brawlstarsinfo.CustomTextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="12sp"
android:textStyle="bold"
app:fontName="supercell.ttf" />
<com.davidbalas.brawlstarsinfo.CustomTextView
android:id="@+id/top5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/top_5_brawlers_for"
android:textColor="@android:color/white"
android:textSize="22sp"
android:textStyle="bold"
app:fontName="supercell.ttf" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/top_5_brawlers_for"
android:src="@drawable/barley" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/top_5_brawlers_for"
android:src="@drawable/barley" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/top_5_brawlers_for"
android:src="@drawable/barley" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/top_5_brawlers_for"
android:src="@drawable/barley" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="@string/top_5_brawlers_for"
android:src="@drawable/barley" />
</LinearLayout>
</LinearLayout>
然而,我得到一个错误,嵌套权重对性能不利。为了达到我想要的目的,有什么好的,另一种方式?
答案 0 :(得分:0)
我会这样解决这个问题;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>