Reputation: 39
Does anyone know how to create LinearLayout in Fragment?
I would like to display TextView, ListView in a layout, so I would like to create it in a LinearLayout.
However, I cannot create the LinearLayout in the class like: LinearLayout ll = new LinearLayout(this);
public class GamesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
return rootView;
}}
This is my XML
<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="#ff8400" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Games Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
Upvotes: 1
Views: 656
Reputation: 5626
If I understand your question correctly, you want to create a LinearLayout inside your fragment's layout file. If there is no reason to create it programatically, then use the xml file like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff8400" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Games Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
That way, when you inflate the layout in your fragment, you will have the linear layout in it already defined. I hope this helped.
Upvotes: 1