简体   繁体   English

多张图片的线性布局,无法正确获取fill_parent/wrap_content

[英]Linear layout of several images, can't get fill_parent/wrap_content right

I have a grid of 9x2 PNG's that I would like to distribute in my widget我有一个 9x2 PNG 的网格,我想在我的小部件中分发

After some tips I decided to go for (cleaned up the code a bit)经过一些提示后,我决定将 go 用于(稍微清理一下代码)

<linearLayout
  android:orientation="vertical">
  <linearLayout
    android:orientation="horizontal">
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
  </linearLayout>
  <linearLayout
    android:orientation="horizontal">
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
      <ImageView />
  </linearLayout>
</linearLayout>

The million dollar question is how to put the width/height of the elements.百万美元的问题是如何放置元素的宽度/高度。 I'd like the object to fill the whole width of the widget, hence width="fill_parent" on the top level.我希望 object 填充小部件的整个宽度,因此 width="fill_parent" 在顶层。 But apart from that I would just like the images to scale nicely.但除此之外,我只希望图像能够很好地缩放。 I can't seem to get that working and wonder if I missed the concept of fill_parent/wrap_content.我似乎无法让它发挥作用,我想知道我是否错过了 fill_parent/wrap_content 的概念。

I tried exeperimenting with the weight parameter but since the images have different sizes this just didn't work out.我尝试使用权重参数进行实验,但由于图像的尺寸不同,所以这没有奏效。

Any help on how to define the width/height of the above elements to make this 9x2 grid fill the width and take up as much space as neccessary on the height would be greatly appreciated!对于如何定义上述元素的宽度/高度以使这个 9x2 网格填充宽度并在高度上占用尽可能多的空间的任何帮助将不胜感激!

Thanks Johan谢谢约翰

You have to use the layout parameter android:layout_weight.您必须使用布局参数 android:layout_weight。 If all imageviews have layout_weight=1 the space will be divided equally among them:如果所有图像视图的 layout_weight=1 空间将在它们之间平均分配:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contentContainer"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

  <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"  android:background="#0000aa">
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
  </LinearLayout>
  <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"  android:background="#00aaaa">
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
      <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent"  android:background="#ff0000"/>
  </LinearLayout>
</LinearLayout>

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

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