【问题标题】:Fragment and android:text:"@string/..."片段和 android:text:"@string/..."
【发布时间】:2014-08-19 17:15:25
【问题描述】:

基本上,我在我的项目中使用 Fragment 并且一切正常

但是我发现无法将value文件夹中的字符串返回到android:text

例子:

android:text="Hello World"

android:text="@string/text1"

谁能告诉我这个问题的解决方案?

我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/text1"
        android:textSize="20dp"
        android:layout_centerInParent="true"
        />


</RelativeLayout>

主要

public class AboutMe extends Fragment {

    @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
        View rootView = inflater.inflate(R.layout.fragment_aboutme, container, false);
        setHasOptionsMenu(true);
        return rootView;
    }

【问题讨论】:

  • 你能展示你的xml布局吗?
  • 我已经发布了xml布局
  • 如果你想以编程方式访问字符串:getActivity().getResources().getString(R.string.text1); 如果你想通过 XML,你做对了。我不知道这种情况下的错误是什么。
  • 您不必包含 getActivity() 来获取字符串,只需 getResources().getString(R.string.text1);也有效

标签: android fragment


【解决方案1】:

您需要通过以下方式访问它:

getActivity().getResources().getString(R.string.text1);

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2012-08-03
    相关资源
    最近更新 更多