我有一个imageview。我想让它的宽度为fill_parent。我想让它的高等于宽度。例如:
<ImageView
android:layout_width="fill_parent"
android:layout_height="whatever the width ends up being" />
在布局文件中,不需要创建自己的视图类,这样的事情可能吗?
谢谢
我有一个imageview。我想让它的宽度为fill_parent。我想让它的高等于宽度。例如:
<ImageView
android:layout_width="fill_parent"
android:layout_height="whatever the width ends up being" />
在布局文件中,不需要创建自己的视图类,这样的事情可能吗?
谢谢
当前回答
如果你的图像视图在一个约束布局中,你可以使用以下约束来创建一个正方形的图像视图,确保使用1:1来制作正方形
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/ivImageView"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
其他回答
ImageView "scaleType"函数可以帮助你。
这段代码将保持图像的纵横比和位置在顶部:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitStart"
这里有一个很棒的帖子展示了使用scaleType的可能性和外观。
ImageView scaleType样本
为了将ImageView设置为屏幕的一半,你需要为ImageView添加以下内容:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
然后将高度设置为宽度,需要在代码中完成。在你的GridView适配器的getView方法中,设置ImageView的高度等于它的测量宽度:
mImageView.getLayoutParams().height = mImageView.getMeasuredWidth();
对于现在路过的人,在2017年,实现你想要的最好的新方法是使用这样的ConstraintLayout:
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1:1" />
不要忘记根据你的布局需要在所有四个方向上添加约束。
用ConstraintLayout构建一个响应式UI
此外,到目前为止,PercentRelativeLayout已经被弃用(参见Android文档)。
我确实喜欢这个-
layout.setMinimumHeight(layout.getWidth());
这里我做了一个ImageButton,它的宽度总是等于它的高度(并避免愚蠢的空白空白在一个方向…我认为这是SDK的一个bug…)
我定义了一个SquareImageButton类,它扩展自ImageButton:
package com.myproject;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageButton;
public class SquareImageButton extends ImageButton {
public SquareImageButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public SquareImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public SquareImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
int squareDim = 1000000000;
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int h = this.getMeasuredHeight();
int w = this.getMeasuredWidth();
int curSquareDim = Math.min(w, h);
// Inside a viewholder or other grid element,
// with dynamically added content that is not in the XML,
// height may be 0.
// In that case, use the other dimension.
if (curSquareDim == 0)
curSquareDim = Math.max(w, h);
if(curSquareDim < squareDim)
{
squareDim = curSquareDim;
}
Log.d("MyApp", "h "+h+"w "+w+"squareDim "+squareDim);
setMeasuredDimension(squareDim, squareDim);
}
}
这是我的xml:
<com.myproject.SquareImageButton
android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="@drawable/icon_rounded_no_shadow_144px"
android:background="#00ff00"
android:layout_alignTop="@+id/searchEditText"
android:layout_alignBottom="@+id/searchEditText"
android:layout_alignParentLeft="true"
/>
效果好极了!