2011-06-21 31 views
4

我有一个图像按钮。如果需要,我可以用自定义视图替换它。我需要将图像按钮的高度指定为Fill_parent。以便图像在屏幕上延伸到可用高度。当我这样做时,我不想宽松。我希望它是一个最小的高度,如果大于高度,那么它不是一个问题。我不想这么久。 虽然我这样做,我也想保持高宽比。请让我知道我需要调整的参数以获得这种观点。在这方面的任何形式的帮助表示赞赏。感谢您的帮助和时间。使图像按钮大小始终为正方形。 Android

编辑:下面是完整的XML中,我想有高度FILL_PARENT水平滚动视图,我试图与符合其高度,并扩大或缩小宽度按图像来填补它长宽比。即使图像很小,我想要放大以使高度始终占据水平滚动视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="vertical" android:layout_weight="3" 
     android:background="#666666"> 
    </LinearLayout> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="vertical" android:layout_weight="7" 
     android:background="#888888"> 
     <HorizontalScrollView android:id="@+id/horizontalScrollView1" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <LinearLayout android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" android:layout_height="fill_parent" 
       android:orientation="horizontal"> 

       <ImageView android:src="@drawable/image1" 
        android:layout_weight="1" android:layout_width="wrap_content" 
        android:layout_height="fill_parent" android:adjustViewBounds="true" 
        android:id="@+id/attachimagebutton_2" /> 

       <ImageView android:src="@drawable/image2" 
        android:layout_weight="1" android:layout_width="wrap_content" 
        android:layout_height="fill_parent" android:adjustViewBounds="true" 
        android:id="@+id/attachimagebutton_2" /> 

      </LinearLayout> 
     </HorizontalScrollView> 
    </LinearLayout> 
</LinearLayout> 

回答

2

那么,有没有必要为它是一个ImageButton,因为你可以在Android的一个按钮什么...

您可以在XML中添加ImageView,给它一个id,图像来源,高度为fill_parent,宽度为wrap_content,并且您使用android:adjustViewBounds="true"修复了宽高比。然后,您还可以添加android:clickable="true",并通过您的id在代码中找到它。然后你可以添加onClickListener

希望工程

+0

你尝试过什么,我写的吗?如果我理解你在说什么,那么代码应该这样做...我的意思是,adjustViewBounds和layout_width属性中的wrap_content将以缩放方式修复宽度 – CodeKrieger

+0

但是,如果容器是容器,如何将图像伸展到300x300 300x72 ...在这种情况下,它会始终是72x72,因为它先填充最小的长度....为什么你把它作为300x72?你可以做得更大吗? 你不能有不同的高度和宽度,因为你有一个正方形,你保持高宽比,再想一想你指的是什么 – CodeKrieger

+0

我的意思就像我看到的结果是300x72。这不是理想的结果。期望的结果是300x300。我的高度设置为“fill_parent”。这大约是300.宽度设置为wrap_content。我希望图像从72x72扩展到300x300。如果图像是600x800,我希望它设置为300x400,因为最大高度为300.但是我的要求中没有最大宽度。这可以做到吗? 也想提一下,高度约为300.它必须设置为fill_parent,而不是特定的dp,因为我需要为所有类型的屏幕尺寸设置。 – Vinodtiru

1

我建议采取不同的方法,有父布局应用gravity="center_horizontal",下一步是有扩展ImageButton类来设置其大小为方形自定义视图,它真的很简单。

因此,创建新的课程,我们将其命名为SquareImageButton,并将其扩展为ImageButton。添加默认构造函数的上下文和属性,让它调用超级函数,你不需要添加任何东西。

现在覆盖onMeasure()方法(它给出了int值的宽度和高度的测量值),取最小值,并且(按照方法要求)用新的最小值调用setMeasuredDimension(w, h),这样它就是平方的。它适用于我,这里是代码,如果你有点丢失:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec); 
    setMeasuredDimension(minDimension, minDimension); 
} 
+0

This将创建一个具有最小长度的设备的正方形视图... –

+0

准确地说,如果您取最大值,那么您可以创建一个按钮,比如允许绘图区域的高度。编辑:我得到你说的,但它已经在不同的布局上为我工作,它不应该返回显示维度,但分配给视图的区域 – Armando

0

试试这个代码,它为我工作。

public class ButtonSquare extends Button { 
public ButtonSquare(Context context) { 
    super(context); 
} 

public ButtonSquare(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ButtonSquare(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 
    super.onMeasure(
      MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 
} 

}