【问题标题】:Buttons have no text on Android 4.0.4 deviceAndroid 4.0.4 设备上的按钮没有文字
【发布时间】:2015-12-19 17:41:09
【问题描述】:

我一直在 Android 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_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;
}

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

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

【问题讨论】:

  • 为什么按钮文字是白色的??
  • @Clairvoyant 因为我使用的@style 很暗。
  • 何时将可见性从已消失更改为可见?
  • @YonatanNir 在另一个片段完成 HTTP 请求之后。它在事件总线上广播从响应中解析的对象,每个按钮片段接收它并设置自己的可见性。按钮本身是可见的,而不是文本。

标签: android android-fragments android-button


【解决方案1】:

而不是这个

button.setText(R.string.member_info);

使用

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

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

编辑: 像下面这样更改您的 xml,然后为您的按钮查找视图byid

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

【讨论】:

  • 没有区别。有一个版本的 setText() 采用资源 ID。
  • 你用于按钮的布局,按钮标签是父标签吗?你能显示你的片段的布局代码吗
  • 更新了我的答案看看
  • 将按钮放在RelativeLayout 中解决了这个问题。我猜片段的视图必须是某种ViewGroup。我仍在使用带有资源 ID 的 setText() 版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多