Android 4.0.4设备上的按钮没有文本
我一直在安卓5.1.1上开发一个应用程序,一切正常。但当我在4.0.4设备上测试它时,没有一个按钮显示任何文本。知道为什么会这样吗 每个按钮都是一个片段的UI。这是名为Android 4.0.4设备上的按钮没有文本,android,android-fragments,android-button,Android,Android Fragments,Android Button,我一直在安卓5.1.1上开发一个应用程序,一切正常。但当我在4.0.4设备上测试它时,没有一个按钮显示任何文本。知道为什么会这样吗 每个按钮都是一个片段的UI。这是名为just_a_button.xml的布局: <?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" style="@style/button_copp
just_a_button.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
按钮的可见性由事件接收器控制。按钮按预期显示,其单击侦听器工作。上面没有文字
编辑:解决方案是将按钮放在布局中。我仍然很好奇这个需求是否有文档记录,为什么当按钮是根视图时,只有文本似乎会受到影响。而不是这个
button.setText(R.string.member_info);
使用
您直接定义字符串资源,但setText()
将string
作为参数
编辑:
如下面所示更改xml,然后为按钮查找dviewById
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Mybutton"
style="@style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
</LinearLayout>
为什么按钮文本颜色为白色???@Clarivoyant,因为我正在使用的@style
是黑色的。什么时候将可见性从gone更改为visible?@YonatanNir在另一个片段完成HTTP请求后。它在事件总线上广播从响应解析的对象,每个按钮片段接收它并设置自己的可见性。按钮本身是可见的,而不是文本。没有区别。有一个版本的setText()
采用了资源ID。您用于按钮的布局,按钮标记是否为父标记??你能为你的碎片显示布局代码吗更新了我的答案检查它输出一个RelativeLayout
内的按钮解决了问题。我猜片段的视图必须是某种视图组。我仍然在使用带有资源ID的setText()
版本。
button.setText(getActivity().getResources().getString(R.string.member_info));
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Mybutton"
style="@style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
</LinearLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.just_a_button, container, false);
Button button = (Button) view.findViewById(R.id.Mybutton);
button.setText(getActivity().getResources().getString(R.string.member_info));
// set click listener omitted
return view;
}