Android 4.0.4 设备上的按钮没有文字

Buttons have no text on Android 4.0.4 device

我一直在 Android 5.1.1 上开发应用程序,一切正常。但是当我在 4.0.4 设备上测试它时,none 按钮显示任何文本。知道为什么会这样吗?

每个按钮都是一个片段的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_copper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:visibility="gone"/>

这是片段如何设置视图的示例:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final Button button = (Button) inflater.inflate(R.layout.just_a_button, container, false);
    button.setText(R.string.member_info);
    // set click listener omitted
    return button;
}

按钮的可见性由事件接收器控制。该按钮按预期显示,并且其点击侦听器有效。只是上面没有文字。

编辑: 解决方案是将按钮放在布局中。我仍然很好奇是否曾经记录过此要求,以及为什么当按钮是根视图时似乎只有文本受到影响。

而不是这个

button.setText(R.string.member_info);

使用

button.setText(getActivity().getResources().getString(R.string.member_info));

您是直接定义字符串资源,但是setText()string作为参数。

编辑: 改变你 xml 如下所示,然后为你的按钮找到 viewbyid

<?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>

在您的片段 onCreateView() 中执行以下操作:

@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;
}