Android | Добавление компонентов программно не работает
Я создаю активность с вкладками в моем приложении для Android. Для этого у меня есть этот фрагмент_основной.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. Не работает тоже) Спасибо!