我正在Android应用中创建一个标签式活动。为此,我有这个fragment_main.xml:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.obware.story.Activitys.MainActivity$MainFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_scrollview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_linear_layout"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/thisid"
android:text="HI"/>
</LinearLayout>
</ScrollView>
这就是添加它:
public static class MainFragment extends Fragment{
public MainFragment(){
}
public static MainFragment newInstance(){
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View mainView = inflater.inflate(R.layout.fragment_main, container, false);
return mainView;
}
}
现在我想添加一些文本来测试它,但它不起作用。在这里我添加它:
public void loadContent(){
LinearLayout layout = (LinearLayout)findViewById(R.id.main_linear_layout);
TextView test = new TextView(this);
test.setText("TEST");
test.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
layout.addView(test);
//Here I tested if I can access it. The layout dissappears, so yes, I can access it.
//layout.setVisibility(View.GONE);
}
那么,有谁知道错误是什么? (我还尝试使用xml文件添加组件。也没有工作)谢谢!