在不使用任何ressource xml的情况下将视图(按钮,标签等)添加到动态片段

首先我要说的是,我想说“你好在这里”。

要求:

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

状态:

我创建了一个相对庞大的开发模型,使用接口和子类(例如按钮),它们都实现了特殊的自己的接口,以便对我的需求做出适当的反应。 我读到了片段,片段活动和片段,我必须使用v4兼容性类,所以我的活动inheritance自FragmentActivity并实现一些特殊的自己的接口。 我现在处于这样的位置,其中一个控制器类,它是我的FragmentActivity类中唯一的引用,做了很多事情,最后应该使片段可见。

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

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

问题:

a)我可以在没有xml-layout的情况下工作,并在编程时和运行时将任何布局应用于片段吗? 我在自定义片段类中使用全局声明的布局的第一次尝试没有成功,因为我想在几个状态期间调用getView()但总是为空。 所以我必须问

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

THX提前。

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文件。

  @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); } }