gpt4 book ai didi

android - 如何在扩展 Fragment 的类中创建一个 LinearLayout?

转载 作者:行者123 更新时间:2023-11-30 02:43:07 27 4
gpt4 key购买 nike

有谁知道如何在 Fragment 中创建 LinearLayout?我想在布局中显示 TextView、ListView,所以我想在 LinearLayout 中创建它。但是,我无法在类中创建 LinearLayout,例如: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;


}}

这是我的 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"/>

最佳答案

如果我正确理解您的问题,您想要在 fragment 的布局文件中创建一个 LinearLayout。如果没有理由以编程方式创建它,那么使用这样的 xml 文件:

 <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>

这样,当您在 fragment 中扩充布局时,您将在其中定义线性布局。我希望这对您有所帮助。

关于android - 如何在扩展 Fragment 的类中创建一个 LinearLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25477215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com