子ImageView动态变化后调整LinearLayout高度
Adjust LinearLayout height after dynamic change of child ImageView
我的问题如下。在我的应用程序中,我有一个验证码查询来验证用户。在DialogFragment
中,假设最初保存验证码的ImageView
是空的,图像是在收到验证码后在代码中动态设置的。
问题是 LinearLayout
高度是在 ImageView
为空时计算的,所以在我设置它之后,图像将所有内容向下推到对话框的不可见区域 window (因为 LinearLayout身高不是updated
).
我很确定这是原因,因为我尝试将图像高度设置为固定值,效果如下。
这是我的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/captcha_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/captcha_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<EditText
android:id="@+id/captcha_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type captcha code here..."
android:maxLength="14"
android:maxLines="1"
android:singleLine="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/button_captcha_refresh" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button_captcha_submit" />
</LinearLayout>
设置验证码图像后如何以某种方式重新呈现 LinearLayout
,以便它考虑新的高度值,有什么想法吗?
编辑:
我忘了说我试图在 LinearLayout 中调用 invalidate
以重新渲染它。
编辑 2:
根据要求,这是我的 DialogFragment 的一部分
public void refreshCaptcha(String b64captcha) {
byte[] decodedString = Base64.decode(b64captcha, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
linear.invalidate();
linear.requestLayout();
}
视图的创建方式如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_captcha, container);
getDialog().setTitle("Catpcha verification");
activity = (LoginActivity) getActivity();
image = (ImageView) view.findViewById(R.id.captcha_image);
linear = (LinearLayout) view.findViewById(R.id.captcha_linear);
captcha_input = (EditText) view.findViewById(R.id.captcha_input);
/* ... button callback events skipped ... */
return view;
}
我解决了。我在 that 博客上找到了解决方案。原来将整个布局包装成 RelativeLayout
解决了这个问题,我真的不知道为什么(如果有人知道我将不胜感激)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/captcha_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/captcha_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<EditText
android:id="@+id/captcha_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type captcha code here..."
android:maxLength="14"
android:maxLines="1"
android:singleLine="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/button_captcha_refresh" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button_captcha_submit" />
</LinearLayout>
</RelativeLayout>
修复后看起来像这样:
我的问题如下。在我的应用程序中,我有一个验证码查询来验证用户。在DialogFragment
中,假设最初保存验证码的ImageView
是空的,图像是在收到验证码后在代码中动态设置的。
问题是 LinearLayout
高度是在 ImageView
为空时计算的,所以在我设置它之后,图像将所有内容向下推到对话框的不可见区域 window (因为 LinearLayout身高不是updated
).
我很确定这是原因,因为我尝试将图像高度设置为固定值,效果如下。
这是我的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/captcha_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/captcha_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<EditText
android:id="@+id/captcha_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type captcha code here..."
android:maxLength="14"
android:maxLines="1"
android:singleLine="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/button_captcha_refresh" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button_captcha_submit" />
</LinearLayout>
设置验证码图像后如何以某种方式重新呈现 LinearLayout
,以便它考虑新的高度值,有什么想法吗?
编辑:
我忘了说我试图在 LinearLayout 中调用 invalidate
以重新渲染它。
编辑 2:
根据要求,这是我的 DialogFragment 的一部分
public void refreshCaptcha(String b64captcha) {
byte[] decodedString = Base64.decode(b64captcha, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
linear.invalidate();
linear.requestLayout();
}
视图的创建方式如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_captcha, container);
getDialog().setTitle("Catpcha verification");
activity = (LoginActivity) getActivity();
image = (ImageView) view.findViewById(R.id.captcha_image);
linear = (LinearLayout) view.findViewById(R.id.captcha_linear);
captcha_input = (EditText) view.findViewById(R.id.captcha_input);
/* ... button callback events skipped ... */
return view;
}
我解决了。我在 that 博客上找到了解决方案。原来将整个布局包装成 RelativeLayout
解决了这个问题,我真的不知道为什么(如果有人知道我将不胜感激)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/captcha_linear"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/captcha_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<EditText
android:id="@+id/captcha_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type captcha code here..."
android:maxLength="14"
android:maxLines="1"
android:singleLine="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"
android:id="@+id/button_captcha_refresh" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button_captcha_submit" />
</LinearLayout>
</RelativeLayout>
修复后看起来像这样: