【问题标题】:Create android TextView inside Fragment class by java code (without .xml)通过java代码在Fragment类中创建android TextView(不带.xml)
【发布时间】:2021-10-01 05:49:43
【问题描述】:

我正在做课堂练习,告诉我:

  • 在名为 ForecastFragment.java 的 Fragment 类中创建一个 TextView,该 Fragment 将显示在 MainActivity.java 中。
  • 不要使用xml文件,全部用.java编码

我的工作:

public class ForecastFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forecast, container, false);
        view.setBackgroundColor(Color.BLUE);

        LinearLayout linearLayout = new LinearLayout(getActivity());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(getActivity());
        String text = "Thursday";
        textView.setText(text);

        linearLayout.addView(textView);
        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".ForecastFragment">

</FrameLayout>

结果:

  • TextView 未添加,片段也未添加 => 100% 失败

问题:

  • 在 LinearLayout 和 TextView 构造函数中,它们需要 Context 实例,但我们在 Fragment 中,因此“this”关键字 die => 我使用 getActivity() 代替。这是真的还是假的?
  • 为什么我的代码不起作用。
  • 我仍然不知道如何将 TextView 约束到父级。
  • 使用java创建元素比xml难很多,所以需要一些源码来理解

谢谢大家。

【问题讨论】:

  • 这是由于您没有将您的 linearLayout 添加到返回的视图中。并且由于它是 View 类的对象,因此您无法添加子尝试使用 ViewGroup,例如 LinearLayout 或其他将 ViewGroup 扩展为父视图而不是视图的视图。
  • 你的意思是将片段添加到 ViewGroup 然后将 ViewGroup 添加到 MainActivity。
  • 不不,你完全错了,你能分享 fragment_forecast.xml 的代码,以便我更好地解释它。
  • 是的,我已经在上面添加了。希望你能帮我解决这个问题。谢谢

标签: java android android-fragments


【解决方案1】:

发生以下事情的原因是您没有将 LinearLayout 对象添加到将在屏幕上呈现的任何父级。到目前为止,它是 View 类的视图对象,不能添加子视图。

所以进行以下更改:

public class ForecastFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_forecast, container, false);
        view.setBackgroundColor(Color.BLUE);

        LinearLayout linearLayout = new LinearLayout(view.getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(view.getContext());
        String text = "Thursday";
        textView.setText(text);

        view.addView(linearLayout);    
        linearLayout.addView(textView);
        return view;
    }
}

现在我将回答您的一些问题: 问题:

  • 在 LinearLayout 和 TextView 构造函数中,它们需要 Context 例如,但我们在片段中,所以“this”关键字死了=>我使用 getActivity() 代替。这是真的还是假的?

它可以工作,但如果你尝试在片段运行时使用 getActivity() 与活动分离,它将返回 null,因此您必须检查每个 关于那个的时间。所以你可以使用 viewObject.getContext() 所以它 不会为空。

  • 为什么我的代码不起作用。

正如我之前解释的那样;它正在工作,但你看不到它导致它 未附加到当前显示的布局。

  • 我仍然不知道如何将 TextView 约束到父级。使用java&创建元素比xml难很多,所以需要一些源码来理解

为此,您必须练习每个 ViewGroup 以及它们如何 管理子视图。你也可以在网上找到很多关于 他们。

【讨论】:

  • 有问题,addView()方法不存在。
  • 您是否使用 FrameLayout 更改了视图对象类。因为 FrameLayout 有方法 addView 并且如果可能的话提供更新的代码。
  • 是的,view.addView() 仍然不存在
  • 我认为它应该有 addView 因为它扩展了 ViewGroup 类,但是我的 IDE 如何为此引发异常:)
  • 哦,谢谢你我修好了。
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 2020-12-07
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多