我有一个imageview.我希望它的宽度为fill_parent.我希望它的高度无论宽度最终如何.例如:
<ImageView
android:layout_width="fill_parent"
android:layout_height="whatever the width ends up being" />
Run Code Online (Sandbox Code Playgroud)
在布局文件中是否可以这样,而不必创建自己的视图类?
谢谢
dav*_*aya 152
更新:2017年9月14日
根据下面的评论,从Android支持库26.0.0开始,不推荐使用百分比支持库.这是实现它的新方法:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
看到这里
推荐使用:
根据Android开发者的这篇文章,您现在需要做的就是在PercentRelativeLayout或PercentFrameLayout中包装您想要的任何内容,然后指定其比率,如此
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:layout_widthPercent="100%"
app:layout_aspectRatio="100%"/>
</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)
小智 93
也许这会回答你的问题:
<ImageView
android:id="@+id/cover_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)
您可以忽略scaleType
此特定情况的属性.
dre*_*ees 30
这可以使用LayoutParams在运行时知道视图宽度后动态设置视图高度.您需要使用Runnable线程才能在运行时获取Views宽度,否则您将在知道View的宽度之前尝试设置Height,因为尚未绘制布局.
我如何解决我的问题的示例:
final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_id);
mFrame.post(new Runnable() {
@Override
public void run() {
RelativeLayout.LayoutParams mParams;
mParams = (RelativeLayout.LayoutParams) mFrame.getLayoutParams();
mParams.height = mFrame.getWidth();
mFrame.setLayoutParams(mParams);
mFrame.postInvalidate();
}
});
Run Code Online (Sandbox Code Playgroud)
LayoutParams必须是视图所在的父视图的类型.我的FrameLayout位于xml文件中的RelativeLayout内.
mFrame.postInvalidate();
Run Code Online (Sandbox Code Playgroud)
被调用以强制视图在与UI线程不同的单独线程上重绘
Reg*_*_AG 13
在这里,我做了一个ImageButton,它的宽度总是等于它的高度(并避免在一个方向上的愚蠢的空边距......我认为这是一个SDK的bug ...):
我定义了一个从ImageButton扩展的SquareImageButton类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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"
/>
Run Code Online (Sandbox Code Playgroud)
奇迹般有效 !
Sha*_*ten 11
我无法得到David Chu的回答来为RecyclerView项目工作,并想出我需要将ImageView约束到父级.将ImageView宽度设置为0dp
并将其开始和结束约束到父级.我不确定在某些情况下是设置宽度wrap_content
还是match_parent
工作,但我认为这是让ConstraintLayout的子节点填充其父节点的更好方法.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="1:1"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
Cha*_*raM 11
如果您的图像视图在约束布局内,您可以使用以下约束来创建方形图像视图,确保使用 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"/>
Run Code Online (Sandbox Code Playgroud)
要将 ImageView 设置为屏幕的一半,您需要将以下内容添加到 ImageView 的 XML 中:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)
要将高度设置为等于该宽度,您需要在代码中执行此操作。在适配器getView
的方法中GridView
,将ImageView
高度设置为其测量的宽度:
mImageView.getLayoutParams().height = mImageView.getMeasuredWidth();
Run Code Online (Sandbox Code Playgroud)
在 Android 26.0.0 PercentRelativeLayout 已被弃用。
解决它的最佳方法现在ConstraintLayout
是这样的:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/you_image"
app:layout_constraintDimensionRatio="1:1"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是有关如何添加到项目的教程ConstraintLayout
。
你不能单独使用布局,我试过了.我最后编写了一个非常简单的类来处理它,你可以在github上查看它.SquareImage.java它是一个更大的项目的一部分,但没有任何一点复制和粘贴无法修复(在Apache 2.0下许可)
基本上你只需要设置高度/宽度等于另一个尺寸(取决于你想要缩放它的方式)
注意:您可以使用该scaleType
属性使其成为正方形而不使用自定义类,但视图的边界超出可见图像范围,如果您将其他视图放在其附近,则会出现问题.
归档时间: |
|
查看次数: |
93376 次 |
最近记录: |