Android 动态更改子图像视图后调整线性布局高度
我的问题如下。在我的应用程序中,我有一个验证码查询来验证用户。在DialogFragment中,假设最初持有验证码的ImageView为空,则在收到验证码后在代码中动态设置图像 问题是,LinearLayout高度是在ImageView为空时计算的,所以在我设置它之后,图像会将所有内容向下推到对话框窗口的不可见区域,因为LinearLayout高度不会更新 我很确定这就是原因,因为我试图将图像高度设置为固定值,结果如下 这是我的布局Android 动态更改子图像视图后调整线性布局高度,android,layout,resize,android-linearlayout,android-imageview,Android,Layout,Resize,Android Linearlayout,Android Imageview,我的问题如下。在我的应用程序中,我有一个验证码查询来验证用户。在DialogFragment中,假设最初持有验证码的ImageView为空,则在收到验证码后在代码中动态设置图像 问题是,LinearLayout高度是在ImageView为空时计算的,所以在我设置它之后,图像会将所有内容向下推到对话框窗口的不可见区域,因为LinearLayout高度不会更新 我很确定这就是原因,因为我试图将图像高度设置为固定值,结果如下 这是我的布局 <?xml version="1.0" encoding
<?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>
以下是创建视图的方式:
@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;
}
我解决了。我在博客上找到了解决方案。事实证明,将整个布局包装到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>
修复后,看起来是这样的:
收到验证码后,您是否尝试在ImageView或LinearLayour上调用RequestLayour?感谢@Kistamushken提供快速反馈。是的,我还试图改变invalidate和requestLayout的顺序。我只在LinearLayout对象上调用了它们,我也应该在ImageView上尝试吗?我只是尝试在两个对象上调用requestLayout,没有调用invalidate,它没有做任何更改。你能提供你的DialogFragment的代码吗?当然,我刚刚更新了帖子,我没有把整个DialogFragment都放进去,但我确实放了onCreateView和更新验证码的方法。
<?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>