textView没有显示在片段中

时间:2014-05-11 20:26:50

标签: android android-fragments textview

所以我试图在列表项的顶部创建一个带有textview的列表片段,但是我无法显示textview。

这是我的列表片段代码

public class VarsFragment extends ListFragment {

private ArrayList<VarData> VarList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    VarList = AllVars.get(getActivity()).getAllVars();
    VarAdapter varAdapter = new VarAdapter(VarList);
    setListAdapter(varAdapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_vars, container, false);
    TextView Header = (TextView) rootView.findViewById(R.id.VarHeader);
    Header.setText("Variables");
    return rootView;
}

private class VarAdapter extends ArrayAdapter<VarData> {
    public VarAdapter(ArrayList<VarData> variables) {
        super(getActivity(), R.layout.list_item, variables);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item, null);
        }
        VarData varData = getItem(position);
        TextView varNameTextView = (TextView) convertView.findViewById(R.id.varstv1);
        varNameTextView.setText(varData.getVarName());
        TextView varValueTextView = (TextView) convertView.findViewById(R.id.varstv2);
        varValueTextView.setText(varData.getVarValue());
        ImageView editIndicator = (ImageView) convertView.findViewById(R.id.editable_indicator);
        if (varData.isEditable())
        {
            editIndicator.setVisibility(View.VISIBLE);
        }
        else
        {
            editIndicator.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
}

public static VarsFragment newInstance(String text) {

    VarsFragment f = new VarsFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}

这是xml的代码

<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity$VarsFragment"
android:gravity="center_horizontal"
android:background="#ffff8800">

<TextView
    android:id="@+id/VarHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

 </LinearLayout>

当我运行我的应用程序时,我的列表会显示,但我的textview仍为空,而不是显示标题“Variables”。

感谢您的帮助

编辑: 我不要只添加

TextView Header;

位于顶部,

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    Header = (TextView) view.findViewById(R.id.VarHeader);
    Header.setText("Variables");
    Header.setVisibility(View.VISIBLE);
}

1 个答案:

答案 0 :(得分:1)

我有类似的问题。在我的情况下,由于操作栏,textview被隐藏了。我在我的片段布局中添加了“paddingTop”来解决这个问题。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/action_bar_height"   
    tools:context="my frag context">

action_bar_height = 56dp(肖像)或48dp(风景)