【发布时间】:2017-06-30 05:55:31
【问题描述】:
我是 Android 新手,我遇到了复选框和文本视图在运行时不刷新的问题。 TextView 必须将颜色从浅灰色更改为浅灰色,并且 Checkbox 必须将其具有的自定义可绘制对象从红色更改为绿色。我已经尝试了分离并附加片段,但它关闭了,也许我不知道将分离和附加片段放在哪里。我已经尝试过 invalidate() 和 requestLayout(),它也在那里关闭。我尝试将它们放在 onCreateView() 中,也许我不应该把它们放在那里。我的片段没有活动。这是我需要更改的图像。它在预览窗口中是绿色的自定义复选框可绘制对象,但在运行时它们是红色的自定义可绘制对象,这是我将它们更改为绿色之前的样子,并且 TextViews 在预览窗口中是浅灰色,但它们是较暗的运行时的灰色,这是他们以前的样子。
public class CheckListFragment extends Fragment{
private RelativeLayout fragment_checklist;
private Fragment fragment = new CheckListFragment();
private Checkbox chkStart, chkDownload;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_checklist, container, false);
fragment_checklist = (RelativeLayout) rootView.findViewById(R.id.fragment_checklist_gather);
chkStart = (CheckBox) rootView.findViewById(R.id.chkStarted);
chkDownload = (CheckBox) rootView.findViewById(R.id.chkDownloaded);
//tried to explicitly make it green but doesn't work.
chkStart.setButtonDrawable(R.drawable.custom_green_checkbox);
chkDownload.setButtonDrawable(R.drawable.custom_green_checkbox);
fragment.getView().invalidate(); // tried these
fragment.getView().requestLayout();// tried these
//tried this. Maybe in the wrong spot.
FragmentTransaction ft = getFragmentManager.beginTransaction();
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
return rootView;
}
}
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<CheckBox
android:id="@+id/chkStarted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_below="@+id/chkInProgess"
android:gravity="top"
android:padding="4dp"
android:checked="true"
android:enabled="false"
android:layout_marginTop="10dp"
android:button="@xml/custom_checkbox_green"/>
<CheckBox
android:id="@+id/chkDownloaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/chkStarted"
android:text="@string/download"
android:gravity="top"
android:padding="4dp"
android:checked="true"
android:enabled="false"
android:button="@xml/custom_checkbox_green"/>
</RelativeLayout>
文本视图在运行时也没有改变颜色,我尝试刷新布局,但它们在运行时没有刷新。它们是它们在预览窗口中所需要的,但它们不是它们在运行时应该是的。任何帮助将非常感激。提前致谢!
【问题讨论】:
-
您在代码中设置
custom_green_checkbox,而不是 xml 中的custom_checkbox_green。 Java 将覆盖 xml 背景。检查这两个是否是相同的文件。 -
其中一个在drawable文件夹中,另一个在xml中
标签: android android-layout checkbox refresh