我想为FragView中的TextView设置一些文本。在给布局膨胀之后,我使用#findViewById(int)检索TextView对象,然后调用它的#setText(String)。但是,它不起作用。
这就是我的代码现在的样子:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.quantityfragment, container, false);
TextView tvpurchasedunits = (TextView) V.findViewById(R.id.tvpurchasedunits);
tvpurchasedunits.setText("units");
return V;
}
这是我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_quantityfragment_svquantity"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:visibility="gone" />
<LinearLayout
style="@style/ff"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<RelativeLayout
style="@style/fw"
android:layout_weight="1"
android:background="#8BC34A"
android:orientation="vertical"
android:paddingBottom="20dp" >
<TextView
android:id="@+id/add_quantityfragment_tvpurchasedquantity"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Bought"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<EditText
android:id="@+id/add_quantityfragment_etpurchasedquantity"
style="@style/edittext"
android:layout_width="fill_parent"
android:layout_below="@+id/add_quantityfragment_tvpurchasedquantity"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="@drawable/layout_border2"
android:digits="0123456789."
android:inputType="numberDecimal"
android:singleLine="true" />
<TextView
android:id="@+id/tvpurchasedunits"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_below="@+id/add_quantityfragment_etpurchasedquantity"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:text=""
android:textColor="#FFFFFF"
android:textSize="16dp" />
<EditText
android:id="@+id/add_quantityfragment_etexpirydate"
style="@style/edittext"
android:layout_width="fill_parent"
android:layout_above="@+id/add_quantityfragment_tvcleardate"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/layout_border2"
android:focusable="false"
android:hint="Expiry Date"
android:inputType="date"
android:singleLine="true"
android:textSize="12dp" />
<TextView
android:id="@+id/add_quantityfragment_tvcleardate"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="@string/clear"
android:textColor="#FFFFFF" />
</RelativeLayout>
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#EEEEEE" />
<LinearLayout
style="@style/ff"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
style="@style/ff"
android:layout_weight="1"
android:background="#FF5722"
android:orientation="vertical" >
<TextView
android:id="@+id/add_quantityfragment_tvusedquantity"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Used"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<EditText
android:id="@+id/add_quantityfragment_etusedquantity"
style="@style/edittext"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/layout_border2"
android:digits="0123456789."
android:inputType="numberDecimal"
android:singleLine="true" />
<TextView
android:id="@+id/add_quantityfragment_tvusedunits"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_below="@+id/add_quantityfragment_etusedquantity"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:text=""
android:textColor="#FFFFFF"
android:textSize="16dp" />
</RelativeLayout>
<TextView
android:id="@+id/add_quantityfragment_tvor"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:gravity="center"
android:minHeight="40dp"
android:text="{Or}" />
<RelativeLayout
style="@style/ff"
android:layout_weight="1"
android:background="#3F51B5"
android:orientation="vertical" >
<TextView
android:id="@+id/add_quantityfragment_tvremainingquantity"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Now in Pantry"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<EditText
android:id="@+id/add_quantityfragment_etremainingquantity"
style="@style/edittext"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/layout_border2"
android:digits="0123456789."
android:inputType="numberDecimal"
android:singleLine="true" />
<TextView
android:id="@+id/add_quantityfragment_tvremainingunits"
style="@style/text_view_body"
android:layout_width="fill_parent"
android:layout_below="@+id/add_quantityfragment_etremainingquantity"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="right"
android:text=""
android:textColor="#FFFFFF"
android:textSize="16dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
未设置文字。我尝试用不同的方法设置它,例如#onActivityCreated(),#onStart()和#onResume(),但我无法这样做。
有什么想法吗?
答案 0 :(得分:2)
但您应return view;
onCreateView(..)
return V;
像:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.quantityfragment, container, false);
TextView tvpurchasedunits = (TextView).findViewById(R.id.tvpurchasedunits);
tvpurchasedunits.setText("units");
return V;
}
答案 1 :(得分:0)
你是如何使用你的片段的?当片段通过将容器指定为null而膨胀时,我们通常不会添加到组中。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.quantityfragment, null, false);
TextView tvpurchasedunits = (TextView) V
.findViewById(R.id.tvpurchasedunits);
tvpurchasedunits.setText("units");
return V;
}