2016-07-06 33 views
0

我試圖在知道屏幕大小以使佈局爲完美的正方形之後更改相對佈局(以及其內部的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> 

謝謝

+1

類型的'LayoutParams'一個'View'也取決於它的父'View',而不是它本身。你的'RelativeLayout'在'LinearLayout'中,'LinearLayout.LayoutParams'也是。 –

回答

1

將最頂端的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> 
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(); 
相關問題