在android布局xml中根据视图宽度设置视图高度
视图是否可能使其高度与宽度相同 或任何其他布局,因为对于矢量图像,必须提供宽度和高度在android布局xml中根据视图宽度设置视图高度,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,视图是否可能使其高度与宽度相同 或任何其他布局,因为对于矢量图像,必须提供宽度和高度 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/googleLoginBtn" android:orientation="horiz
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/googleLoginBtn"
android:orientation="horizontal"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="<height should be equal to the width>"
app:srcCompat="@drawable/simple"
tools:ignore="MissingPrefix"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Snappy"
android:textColor="#FF4081"
android:textSize="30dp"
android:singleLine="true"
android:gravity="center"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Type less, do more. Fastest way to transfer money & make other transactions."
android:layout_margin="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/simple"
tools:ignore="MissingPrefix"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Smart"
android:textColor="#FF4081"
android:textSize="30dp"
android:gravity="center"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Type less, do more. Fastest way to transfer money & make other transactions."
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
更新: 我将使用一个“今日”并使用属性
app:layout\u constraintDimensionRatio=“1:1”
旧答案:
如果不使用自定义的ImageView,就不可能完全在xml中实现。这将是一个解决方案:
public class SquareImageView extends ImageView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
但是,如果您的图像已经是正方形,则可以使用普通的ImageView
:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
更新: 我将使用一个“今日”并使用属性
app:layout\u constraintDimensionRatio=“1:1”
旧答案:
如果不使用自定义的ImageView,就不可能完全在xml中实现。这将是一个解决方案:
public class SquareImageView extends ImageView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
但是,如果您的图像已经是正方形,则可以使用普通的ImageView
:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true" />
您应该能够为此使用。 您可以将
LinearLayout
中的ImageView
替换为a,并将其他视图android:layout_置于下方,或者将ImageView
包装为a
我假设您已经在这个文件中将应用程序名称空间定义为xmlns:app=”http://schemas.android.com/apk/res-auto“
因为我看到您在使用名称空间
一旦包含了适当的支持库,PercentFrameLayout
方法将如下所示:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<android.support.percent.PercentFrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
app:layout_aspectRatio="100%"
app:srcCompat="@drawable/simple"
tools:ignore="MissingPrefix"/>
</android.support.percent.PercentFrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Snappy"
android:textColor="#FF4081"
android:textSize="30dp"
android:singleLine="true"
android:gravity="center"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Type less, do more. Fastest way to transfer money & make other transactions."
android:layout_margin="10dp"/>
</LinearLayout>
您应该能够使用。
您可以将LinearLayout
中的ImageView
替换为a,并将其他视图android:layout_置于下方,或者将ImageView
包装为a
我假设您已经在这个文件中将应用程序名称空间定义为xmlns:app=”http://schemas.android.com/apk/res-auto“
因为我看到您在使用名称空间
一旦包含了适当的支持库,PercentFrameLayout
方法将如下所示:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<android.support.percent.PercentFrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
app:layout_aspectRatio="100%"
app:srcCompat="@drawable/simple"
tools:ignore="MissingPrefix"/>
</android.support.percent.PercentFrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Snappy"
android:textColor="#FF4081"
android:textSize="30dp"
android:singleLine="true"
android:gravity="center"
android:layout_margin="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Type less, do more. Fastest way to transfer money & make other transactions."
android:layout_margin="10dp"/>
</LinearLayout>
只是一个小小的更新:PercentFrameLayout类在API级别26.1.0中被弃用。考虑使用约束布局和相关布局。此处的详细信息:只是一个小小的更新:PercentFrameLayout类在API级别26.1.0中被弃用。考虑使用约束布局和相关布局。更多信息请点击此处:使用
使用
可能重复的可能重复我使用的是矢量图像,这就是为什么第二个解决方案不适合。一个简单的问题,大小每次更改时是否调用onMeasure()
函数?onMeasure
每次父视图需要更改其布局时都会调用。所以是的。它甚至可以被调用多次。我使用的是矢量图像,这就是为什么第二个解决方案不适合。一个简单的问题是,onMeasure()
函数是否每次大小更改时都被调用?onMeasure
每次父视图需要更改其布局时都被调用。所以是的。它甚至可以被调用多次。