Android:强制更新布局
我正在尝试更改 ImageView 的背景颜色,但只有在单击它后它才会更新。我尝试执行 layout.invalidate()
、layout.postInvalidate()
,其中布局是布局 xml 文件中的根元素:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_select_page_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/color_select_page_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="10px"
android:paddingBottom="10px"
android:text="Установки цвета:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/color_select_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/color_select_simple_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Цвет фона:"
android:paddingRight="20px"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/color_select_background_color"
android:layout_width="170sp"
android:layout_height="30sp"
android:background="#80111111"
/>
</LinearLayout>
<TextView
android:id="@+id/color_select_titles_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет списка юзеров"
android:textSize="30dp" />
<Button
android:id="@+id/color_select_buttons_background_color"
android:paddingTop="20px"
android:paddingBottom="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Цвет фона кнопок" />
<Button
android:id="@+id/color_select_buttons_text_color"
android:layout_marginTop="10px"
android:paddingTop="20px"
android:paddingBottom="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Цвет текста кнопок" />
<TextView
android:id="@+id/color_select_pages_titles_text_color"
android:layout_marginTop="10px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Цвет заголовков страниц"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/color_select_podzagolovki_text_color"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_marginTop="10px"
android:layout_height="wrap_content"
android:text="Цвет подзаголовков страниц"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/color_select_phone_numbers_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет телефонных номеров" />
<TextView
android:id="@+id/color_select_all_other_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет остального мелкого текста" />
<EditText
android:id="@+id/color_select_edit_field_text_color"
android:layout_marginTop="10px"
android:text="Цвет текста полей ввода"
android:layout_width="fill_parent"
android:editable="false"
android:layout_height="wrap_content" >
</EditText>
<EditText
android:id="@+id/color_select_edit_field_text_color"
android:layout_marginTop="10px"
android:text="Цвет фона полей ввода"
android:editable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
<Button
android:id="@+id/color_select_save_changes"
android:layout_marginTop="30px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Сохранить изменения" />
<Button
android:id="@+id/color_select_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Отмена" />
<Button
android:id="@+id/color_select_restore_defaults"
android:layout_marginTop="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Восстановить умолчания" />
</LinearLayout>
</ScrollView>
所以..我不知道该做什么做。有人可以帮我提供建议吗?提前致谢。
I'm trying to change background color of ImageView, but it updates only after I click on it. I tried to do layout.invalidate()
, layout.postInvalidate()
where layout is my root element in layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_select_page_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/color_select_page_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="10px"
android:paddingBottom="10px"
android:text="Установки цвета:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/color_select_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/color_select_simple_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Цвет фона:"
android:paddingRight="20px"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/color_select_background_color"
android:layout_width="170sp"
android:layout_height="30sp"
android:background="#80111111"
/>
</LinearLayout>
<TextView
android:id="@+id/color_select_titles_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет списка юзеров"
android:textSize="30dp" />
<Button
android:id="@+id/color_select_buttons_background_color"
android:paddingTop="20px"
android:paddingBottom="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Цвет фона кнопок" />
<Button
android:id="@+id/color_select_buttons_text_color"
android:layout_marginTop="10px"
android:paddingTop="20px"
android:paddingBottom="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Цвет текста кнопок" />
<TextView
android:id="@+id/color_select_pages_titles_text_color"
android:layout_marginTop="10px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Цвет заголовков страниц"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/color_select_podzagolovki_text_color"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_marginTop="10px"
android:layout_height="wrap_content"
android:text="Цвет подзаголовков страниц"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/color_select_phone_numbers_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет телефонных номеров" />
<TextView
android:id="@+id/color_select_all_other_text_color"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:gravity="center"
android:text="Цвет остального мелкого текста" />
<EditText
android:id="@+id/color_select_edit_field_text_color"
android:layout_marginTop="10px"
android:text="Цвет текста полей ввода"
android:layout_width="fill_parent"
android:editable="false"
android:layout_height="wrap_content" >
</EditText>
<EditText
android:id="@+id/color_select_edit_field_text_color"
android:layout_marginTop="10px"
android:text="Цвет фона полей ввода"
android:editable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
<Button
android:id="@+id/color_select_save_changes"
android:layout_marginTop="30px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Сохранить изменения" />
<Button
android:id="@+id/color_select_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Отмена" />
<Button
android:id="@+id/color_select_restore_defaults"
android:layout_marginTop="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Восстановить умолчания" />
</LinearLayout>
</ScrollView>
So.. I don't know what to do. Could anyone help me with advice? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
ImageView
的可见性设置为invisible
,然后再次设置为visible
。这不是一个很好的解决方案,但它在invalidate()
、requestLayout()
等不再起作用的时候对我有用。Try setting the visibility of your
ImageView
toinvisible
and then tovisible
again. This is not a nicve solution but it worked for me in times whereinvalidate()
,requestLayout()
and such do not work anymore.