【问题标题】:How can I specify that this ImageView have to be shown smaller?如何指定这个 ImageView 必须显示得更小?
【发布时间】:2016-09-04 14:31:21
【问题描述】:

我是 Android 开发的新手,在我的第一个应用程序中发现以下问题。

我的问题与将图像占用的空间更改为我的布局有关。

所以我遇到了这样的情况:我没有将图像直接放入我的 XML 布局文件中,而是以编程方式将其放入调用此方法的活动代码中:

ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);

difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(context, 3)));

所以 ImgUtility.createRankingImg(context, 3) 返回一个 Bitmap 对象,该对象设置到我的布局的 ImgeView 中,具有 difficultyContainer 作为 ID。

它工作正常,图像显示在我的布局中,这个:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:scaleType="fitXY" />

<TextView
    android:id="@android:id/text1"
    style="@style/pastaTitleTextStyle" />

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

    <LinearLayout
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            style="@style/HeaderTextStyle"
            android:text="Difficoltà:" />


        <ImageView
            android:id="@+id/difficultyContainer"
            android:layout_width="fill_parent"
            android:layout_height="55dp"
            android:scaleType="fitXY" />

    </LinearLayout>

</LinearLayout>

图片正确显示在这个ImageView中:

<ImageView
    android:id="@+id/difficultyContainer"
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:scaleType="fitXY" />

唯一的问题是这样做的图像水平占据了视图的所有空间,这是我得到的(图像是最后一个包含厨师帽的图像):

这应该取决于我使用 android:layout_width="fill_parent" 来表示 ImageView 具有 id=difficultyContainer必须水平占据其容器的所有空间(主 LinearLayout)。

所以我想知道如何为这个 ImageView 设置百分比宽度\高度。

指定百分比以使我的图像在我的应用中显示得更小的最佳方法是什么?最好的解决方案是什么?

【问题讨论】:

  • 我得到的是你想手动设置图像视图的宽度(通过编程方式)?
  • 我想你应该可以搜索“percent layout android”...stackoverflow.com/a/32168421/2308683
  • @dex 不,我想在我的 XML 定义中这样做
  • 你能告诉我们你希望图像是什么样的

标签: java android android-layout android-imageview android-bitmap


【解决方案1】:

您是想问如何以编程方式设置 imageView 的大小吗?

如果是这样使用:

ImageView imageView = findViewById(R.id.difficultyContainer);
imageView.getLayoutParams().width = 120;

高度也一样,只是把宽度换成高度。

这个 '120' 的值在 dp 中,这意味着图像的大小将根据输出设备的屏幕大小进行调整,这意味着您不需要像在 html/css 中那样使用百分比

【讨论】:

  • 好的,很有趣,所以如果我使用 DP 在 XML 视图定义中设置宽度,它与具体使用的显示器有关,我可以避免指定百分比?
  • 是的 :) 1 dp = 设备屏幕上一个物理像素的大小,因此这些大小会因设备而异,请在“密度独立性”部分找到更多信息:developer.android.com/guide/practices/screens_support.html
【解决方案2】:

只需向 ImageView 添加填充

<ImageView
        android:id="@+id/difficultyContainer"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:padding="8dp"
        android:scaleType="fitXY" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多