我正在使用PercentFrameLayout
,因为它有助于响应式设计。我定义的每个尺寸都有%
后缀。但在某些情况下,我必须将大小定义为px
。我的XML具有layout_heightPercent
和layout_widthPercent
属性。我无法使用layout_width
属性将宽度设置为px
。它也必须以编程方式。
我的XML
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#b00032"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#136"
android:id="@+id/imageView"
app:layout_heightPercent="80%"
app:layout_widthPercent="100%"
app:layout_marginTopPercent="17%"
app:srcCompat="@drawable/tr2"
app:layout_marginLeftPercent="0%"/>
</android.support.percent.PercentFrameLayout>
我的代码用于将大小更改为百分比
ImageView map = (ImageView)findViewById(R.id.imageView);
PercentFrameLayout.LayoutParams params =(PercentFrameLayout.LayoutParams) map.getLayoutParams();
info = params.getPercentLayoutInfo();
inforec.widthPercent = 0.5f;
现在我想将其设置为像素而不是百分比。有什么建议吗?