将视图(按钮、标签等)添加到动态片段,而无需使用任何资源 xml



首先我想说,我想说"你好"。

要求:

我应该可以创建一个客户端应用程序,该应用程序从数据库中获取有关控件的元数据。此应用程序应该能够从一个视图(带有子视图,例如按钮)切换到另一个视图。

地位:

创建了一个相对庞大的开发 oo 模型,使用接口和子类(例如按钮),它们都实现了特殊的自己的接口,以便正确响应我的需求。我阅读了有关片段,片段活动和片段的信息,我必须使用v4兼容性类,所以我的活动继承自FragmentActivity并实现了一些特殊的自己的接口。我现在处于一个临界点,控制器类(这是我的 FragmentActivity 类中唯一的引用)执行许多操作,最终应该使片段可见。

我也已经将这些子视图(按钮、标签、文本视图)收集在一个集合中,每个运行时创建的片段现在应该"将其子视图"放在屏幕上。请记住,我的自定义视图"Fragment"继承自Fragment并实现了一些特殊的东西。

我的片段是在运行时创建的,所以我没有任何定义任何布局的 xml。

问题:

a) 我是否可以在没有 XML 布局的情况下工作并以编程方式和运行时将任何布局应用于片段?我第一次尝试使用在我的自定义片段类中全局声明的布局没有成功,因为我想在几种状态下调用 getView(),但总是得到 null。因此,我必须问

b) 问题 b(仅当 a == true 时)何时以及如何从 getView 接收正确的视图以便以编程方式设置布局?

提前感谢。

package com.example.test;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentExample extends android.app.Fragment
{
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
        //Set a linearLayout to add buttons
        LinearLayout linearLayout = new LinearLayout(getActivity());
        // Set the layout full width, full height
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL
        Button button = new Button(getActivity());
        //For buttons visibility, you must set the layout params in order to give some width and height: 
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        Button button2 = new Button(getActivity());
                button2.setLayoutParams(params);
        //... and other views
        ViewGroup viewGroup = (ViewGroup) view;
        linearLayout.addView(button);
        linearLayout.addView(button2);
        viewGroup.addView(linearLayout);
    }
}

我遇到了同样的问题,令我惊讶的是,我找到了解决方案!创建一个空白的 XML 布局文件,如下所示...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</FrameLayout>

在动态创建布局的片段中,膨胀此空白布局 XML 文件。

  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup     container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.blank_layout, container, false);
    return view;

后记,在 onViewCreated() 方法中,您可以动态创建布局。

  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
// This will create the LinearLayout
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
// Configuring the width and height of the linear layout.
LinearLayout.LayoutParams llLP = new LinearLayout.LayoutParams(
        //android:layout_width="match_parent" an in xml
        LinearLayout.LayoutParams.MATCH_PARENT,
        //android:layout_height="wrap_content"
        LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llLP);
TextView tv = new TextView(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
//android:text="@string/c4r"
tv.setText("Hello android !");
//android:padding="@dimen/padding_medium"
tv.setPadding(8, 8, 8, 8);
ll.addView(tv);
ViewGroup viewGroup = (ViewGroup) view;
viewGroup.addView(ll);
}
}

最新更新