android - How to calculate and set Height correctly in onMeasure -
i have custom view, xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <edittext android:id="@+id/post_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/post_et_hint" android:background="@android:color/transparent" android:inputtype="text|textmultiline|textcapsentences|textnosuggestions" android:gravity="center" android:textsize="@dimen/post_et_text_size" android:textstyle="bold" android:layout_marginleft="@dimen/post_et_horizontal_margin" android:layout_marginright="@dimen/post_et_horizontal_margin" android:elevation="2dp" app:layout_constrainttop_totopof="parent" app:layout_constraintright_torightof="parent" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" /> <imageview android:id="@+id/image_view_top" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constrainttop_totopof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" /> <imageview android:id="@+id/image_view_mid" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constraintbottom_totopof="parent" app:layout_constraintbottom_tobottomof="parent" /> <imageview android:id="@+id/image_view_bot" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintright_torightof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintbottom_tobottomof="parent" /> </merge>
and source code (only part):
public class customimageview extends constraintlayout { //... @override protected void onmeasure(final int widthmeasurespec, final int heightmeasurespec) { int overheight = calculaterest(); int overwidth = calculatewidth(); if (mmode == postpresenter.mode.post) { super.onmeasure(widthmeasurespec, widthmeasurespec); timber.i("onmeasure h %d, w %d", overheight, overwidth); if (overwidth > overheight) { timber.i("onmeasure not"); setmeasureddimension(overwidth, overheight); } else if (overwidth <= overheight) { timber.i("onmeasure square"); setmeasureddimension(overwidth, overwidth); } } else { super.onmeasure(widthmeasurespec, heightmeasurespec); } } private int calculatewidth() { return ((constraintlayout) getparent()).getwidth(); } private int calculaterest() { int overheight = 0; overheight += ((constraintlayout) getparent()).getheight(); (int = 0; < ((constraintlayout) getparent()).getchildcount(); i++) { int minusheight = 0; if (((constraintlayout) getparent()).getchildat(i) instanceof tablayout) { minusheight = (((constraintlayout) getparent()).getchildat(i)).getheight(); overheight -= minusheight; } else if (((constraintlayout) getparent()).getchildat(i) instanceof bottombar) { minusheight = ((constraintlayout) getparent()).getchildat(i).getheight(); overheight -= minusheight; } } return overheight; } //... }
and need when closed keyboard displayed square (the place enough) , when keyboard open rectangle not square not enough space (as can see in method calculaterest remove height of 2 elements displayed).
but have next problem: edittext view showing appears off-center after open keyboard, this:
and if chow image image not scaling new center too. how make onmeasure correctly, or need add in xml, inject view? :
?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.design.widget.tablayout android:id="@+id/post_tab_layout" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginleft="92dp" android:layout_marginright="92dp" app:tabmode="fixed" android:scaley="-1" android:elevation="2dp" app:tabtextcolor="@color/post_tab_not_selected_text" app:tabselectedtextcolor="@color/post_tab_selected_text" app:layout_constrainttop_totopof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent"> <android.support.design.widget.tabitem android:id="@+id/post_tab_item_post" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tab_post"/> <android.support.design.widget.tabitem android:id="@+id/post_tab_item_history" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tab_history"/> </android.support.design.widget.tablayout> <imagebutton android:id="@+id/post_toolbar_left_button" android:src="@drawable/ic_toolbar_font" android:elevation="2dp" style="@style/imagebuttontoolbar" app:layout_constrainttop_totopof="parent" app:layout_constraintleft_toleftof="parent" /> <imagebutton android:id="@+id/post_toolbar_right_button" android:src="@drawable/ic_toolbar_sticker" android:elevation="2dp" style="@style/imagebuttontoolbar" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> <view android:id="@+id/post_devider_top" android:layout_width="match_parent" android:layout_height="@dimen/post_view_devider_height" android:elevation="2dp" android:background="@color/black_with_big_transperent" app:layout_constrainttop_tobottomof="@id/post_tab_layout" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent"/> <com.ng.app.ui.view.customimageview android:id="@+id/post_image_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaletype="centercrop" app:layout_constraintright_torightof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_tobottomof="@id/post_devider_top" app:layout_constraintbottom_totopof="@id/post_bottom_bar"/> <com.ng.app.ui.view.bottombar android:id="@+id/post_bottom_bar" android:layout_width="match_parent" android:layout_height="@dimen/bottom_bar_height" android:elevation="2dp" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" /> </android.support.constraint.constraintlayout>
Comments
Post a Comment