在不使用任何ressource xml的情况下将视图(按钮,标签等)添加到动态片段 [英] Add views( buttons, labels, etc ) to a dynamic fragment without using any ressource xml

查看:107
本文介绍了在不使用任何ressource xml的情况下将视图(按钮,标签等)添加到动态片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First of all I would like to say, that I want to say "Hello in here".

要求:

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

I should make it possible to create a client application which gets metadata about controls from a database. This application should be able to switch from one view (with subviews, like buttons for example ) to another view.

状态:

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

I created a relatively huge development oo model, using interfaces, and subclasses (of button, for example), which all implement special own interfaces in order to react properly to my demands. I read about fragment, fragmentactivity and fragments, i must use v4 compatibility classes, so my activity inherits from FragmentActivity and implements some special own interfaces. I am now at the point, where a controller class, which is the only reference in my FragmentActivity class, does many things and finally should make the fragment visible.

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

I also have already collected those subviews(buttons, labels, textviews ) in a collection, and each runtime created fragment should now "place its subviews" onto the screen. Remember, my custom view "Fragment" inherits from Fragment and implements some special things.

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

My fragment is getting created at runtime, so I do not have any xml which defines any layout.

问题:

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

a) is it possible for me working without an xml-layout and apply any layout to the fragment programatically and at runtime ? My first attempts using an Layout declared globally in my custom fragment class did not succeed because I wanted to call getView() during several states BUT always got null. Therefore I must ask

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

b) question b ( only when a == true ) when and how can I receive the correct view from getView in order to set the layout programatically ?

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

这篇关于在不使用任何ressource xml的情况下将视图(按钮,标签等)添加到动态片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆