我已经尝试在知道屏幕大小后改变相对布局(以及其内部的ImageView)的大小,以制作完美的正方形作为布局,但我做错了什么
有人可以帮我一把吗?
我的尺码是:
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
rl_camera = (RelativeLayout) view.findViewById(R.id.rl_camera);
iv_thumbnail = (ImageView)view.findViewById(R.id.iv_thumbnail);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)rl_camera.getLayoutParams();
params.height = metrics.heightPixels;
rl_camera.setLayoutParams(params);
iv_thumbnail.setMinimumHeight(metrics.heightPixels);
rl_camera.invalidate();
iv_thumbnail.invalidate();
该应用程序崩溃:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams...
就行:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)rl_camera.getLayoutParams();
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="@dimen/textsize_normal"
android:text="@string/IMPORTE_TITLE_STRING"
android:visibility="visible"
android:textColor="@android:color/black"
android:layout_margin="16dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:id="@+id/ll_amount"
android:visibility="visible"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp">
<EditText
android:layout_width="match_parent"
android:layout_height="64dp"
android:inputType="numberDecimal"
android:id="@+id/etAmount"
android:textSize="@dimen/textsize_normal"
android:typeface="normal"
android:minWidth="128dp"
android:ellipsize="end"
android:singleLine="true"
android:padding="8dp"
android:layout_gravity="center"
android:hint="@string/IMPORTE_PLACEHOLDER_STRING"
android:gravity="center"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="@string/euro_symbol"
android:id="@+id/tvEuroSymbol"
android:textSize="32sp"
android:visibility="visible"
android:gravity="center"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_weight="7" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:id="@+id/rl_camera"
android:background="@color/app_disable">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/FOTO_TICKET_STRING"
android:id="@+id/tvCameraInfo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_thumbnail"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_zoomed"
android:scaleType="fitCenter"
android:visibility="gone"
android:adjustViewBounds="true"
android:layout_gravity="center" />
</LinearLayout>
谢谢
答案 0 :(得分:1)
将Top most LinearLayout更改为RelativeLayout。另外,要使相机视图位于内部布局下方,请使用layout_below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="@dimen/textsize_normal"
android:text="@string/IMPORTE_TITLE_STRING"
android:visibility="visible"
android:textColor="@android:color/black"
android:layout_margin="16dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:id="@+id/ll_amount"
android:layout_below="@id/textview"
android:visibility="visible"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp">
<EditText
android:layout_width="match_parent"
android:layout_height="64dp"
android:inputType="numberDecimal"
android:id="@+id/etAmount"
android:textSize="@dimen/textsize_normal"
android:typeface="normal"
android:minWidth="128dp"
android:ellipsize="end"
android:singleLine="true"
android:padding="8dp"
android:layout_gravity="center"
android:hint="@string/IMPORTE_PLACEHOLDER_STRING"
android:gravity="center"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="@string/euro_symbol"
android:id="@+id/tvEuroSymbol"
android:textSize="32sp"
android:visibility="visible"
android:gravity="center"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_weight="7" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:id="@+id/rl_camera"
android:layout_below="@id/ll_amount"
android:background="@color/app_disable">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/FOTO_TICKET_STRING"
android:id="@+id/tvCameraInfo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_thumbnail"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_zoomed"
android:layout_below="@id/rl_camera"
android:scaleType="fitCenter"
android:visibility="gone"
android:adjustViewBounds="true"
android:layout_gravity="center" />
</RelativeLayout>
答案 1 :(得分:0)
用以下代码替换代码的最后几行:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)rl_camera.getLayoutParams();
params.height = metrics.heightPixels;
params.width = params.height; // to shape the layout as a square
rl_camera.setLayoutParams(params);
rl_camera.requestLayout();