与code创建布局 [英] Creating layout with code

查看:185
本文介绍了与code创建布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,创建具有code布局。我只是想补充我的线性布局的按钮对象,但它不会产生任何按钮。

下面是我的onCreate方法:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    的LinearLayout布局=(的LinearLayout)findViewById(R.id.layout);    Button按钮=新按钮(本);
    button.setText(R.string.click_me);    layout.addView(按钮);    如果(savedInstanceState == NULL){
        getFragmentManager()调用BeginTransaction()
                。新增(R.id.container,新PlaceholderFragment())
                。承诺();
    }
}

我fragment_main.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_margin =10dp
机器人:ID =@ + ID /布局
机器人:方向=垂直>

***更新*** 1

因此​​,对布局添加对象的时候,当你有一个片段XML它必须在片段类。感谢@Fllo指出了这一点。有没有想到这样的。 :(

下面是我更新code在上添加PlaceHolderFragment类的按钮。

 公共静态类PlaceholderFragment扩展片段{    公共PlaceholderFragment(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
        的LinearLayout布局=(的LinearLayout)rootView.findViewById(R.id.layout);
        Button按钮=新按钮(getActivity());
        button.setText(R.string.click_me);
        layout.addView(按钮);
        返回rootView;
    }
}


解决方案

看来你建立你的看法里面的 fragment_main.xml 而非 activity_main.xml中

第一个的创建一个新的Andr​​oid项目,你有自动创建和打开这些文件:

然后,当你开始,你添加视图的(例如:一个TextView)的内部 fragment_main.xml 文件,而你试图做一个基本事件与活动里面这个观点使用这种布局:

 的setContentView(R.layout.activity_main); //使用activity_main.xml中的文件

您不能运行你的应用程序或有时你只有一个白色的屏幕,因为你尝试调用/显示器的意见是入错了布局。


  

解决方案:移动你所有的东西onCreateView方法进入片段类中。打电话的意见和做一些相关的片段,而不是父活动



把你所有的东西,你的 onCreateView 方法内到您的 PlaceholderFragment 类,如下所示:

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);    //不要忘记您的视图连接到布局
    的LinearLayout布局=(的LinearLayout)rootView.findViewById(R.id.layout);
    //连接上下文
    Button按钮=新按钮(getActivity());
    //添加布局PARAMS
    button.setLayoutParams(新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    //设置文本
    button.setText(R.string.click_me);
    //添加视图
    layout.addView(按钮);    返回rootView;
}

I have problem with creating layout with code. I'm just trying to add a button object on my linear layout but it doesn't create any button.

Here's my onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

    Button button = new Button(this);
    button.setText(R.string.click_me);

    layout.addView(button);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

My fragment_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:id="@+id/layout"
android:orientation="vertical" >

***update 1***

So when adding objects on layout when you have a fragment xml it must be on the fragment class. Thanks to @Fllo for pointing that out. Haven't thought of that way. :(

Here's my updated code in adding the button on PlaceHolderFragment class.

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout);
        Button button = new Button(getActivity());
        button.setText(R.string.click_me);
        layout.addView(button);
        return rootView;
    }
}

解决方案

It seems you built your views inside fragment_main.xml and not activity_main.xml.

When you first create a new android project, you have these files which are automatically created and opened:

Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml file whereas you tried to do a basically event with this view inside your Activity which uses this layout:

setContentView(R.layout.activity_main); // using the activity_main.xml file

You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..

Solution: Move all your stuff inside onCreateView method into the Fragment class. Call views and do something in the related fragment and not the parent activity.


Move all your stuff inside your onCreateView method into your PlaceholderFragment class, as follows:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);  

    // Don't forget to attach your view to the layout
    LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout);
    // Attach the context
    Button button = new Button(getActivity());
    // Add the layout params
    button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    // Set the text
    button.setText(R.string.click_me);
    // Add the view
    layout.addView(button);  

    return rootView;
}

这篇关于与code创建布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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