【问题标题】:Android: setpadding not working with imageviewAndroid:setpadding 不适用于 imageview
【发布时间】:2015-04-25 22:35:29
【问题描述】:

我有两个图像视图,我想在用户点击它们时创建一个类似边框的效果。 我已将两个图像视图的背景设置为彩色。现在,当用户点击图像时,我在 imageview 上设置了一些填充,以便它创建类似边框的效果。但是,不幸的是它没有发生。

这是我的 java 代码:

public void imageClick(View view)
{
switch(view.getId())
{
case (R.id.imageView1):
     image1.setPadding(10,10,10,10);
     image2.setPadding(0,0,0,0);
     break;
case (R.id.imageView2):
     image1.setPadding(0,0,0,0);
     image2.setPadding(10,10,10,10);
     break;
}
}

我不明白为什么单击图像时什么都没有发生。我都试过了

android:cropToPadding = "true"

android:cropToPadding = "false"

但同样没有结果。

我的 imageViews xml 代码:

<ImageView
                android:layout_width="0dp"
                android:id="@+id/imageRegion1"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="imageClick"
                    android:cropToPadding="false"
                    android:background="#fffff300" />

                <ImageView
                    android:id="@+id/imageRegion2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:onClick="imageClick"
                    android:cropToPadding="false"
                    android:background="#fffff300" />

我通过以下方式动态设置图像视图:

image1.setImageBitmap(bitmap1);
image2.setImageBitmap(bitmap2);

【问题讨论】:

  • 将 android:layout_height="match_parent" 更改为 android:layout_height="wrap_content" 它应该可以完美运行
  • 好吧,我试试。但是如果我想要 "match_parent" 而不是 wrap_content 怎么办?
  • 它为总根视图应用填充。所以它不影响indivisual
  • 我试过了,但是没用!
  • 检查答案并尝试同样的方法

标签: android imageview padding


【解决方案1】:

您似乎很可能使用 weightSum 和 layout_weight 机制来平均或合理地计算 imageview 的宽度。

如果我在这里,在这种情况下,当您设置填充时,它不会对已经膨胀的视图进行任何更改。正如您所期望的那样,设置填充值会减小宽度和高度。但它不会。因为,由于重量计算,它将采用先前的宽度/高度值,因此将忽略填充。

希望这能帮助您解决问题。

【讨论】:

    【解决方案2】:
     <LinearLayout
        android:id="@+id/row_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="visible" >
    
        <ImageView
            android:id="@+id/imageRegion1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#fffff300"
            android:cropToPadding="false"
            android:onClick="imageClick" />
    
        <ImageView
            android:id="@+id/imageRegion2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#fffff300"
            android:cropToPadding="false"
            android:onClick="imageClick" />
    </LinearLayout>
    

    我在上面试过了。它对我来说很好用。再试一次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多