使用XML布局作为模板以编程方式创建android按钮 [英] Create android buttons programmatically using XML layout as template

查看:102
本文介绍了使用XML布局作为模板以编程方式创建android按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含TextView的LinearLayout,并且将始终如此.在TextView下方始终至少会有一个按钮,但是在某些情况下可能会有多个按钮.

I have a LinearLayout that contains a TextView, and always will. There will also always be at least one button located below the TextView, but there might be more than one under certain circumstances.

我可以通过编程成功创建和添加任意数量的按钮.我还可以通过编程成功设置这些按钮所需的任何与外观相关的参数/选项.

I can successfully create and add as many buttons as I need programmatically. I can also successfully set whatever appearance related parameters/options that I require for these buttons programmatically.

问题是我不知道如何告诉以编程方式创建的按钮它应该使用XML资源文件,该文件包含外观和布局参数,而不是通过编程方式设置这些参数.

The problem is that I don't know how to tell a programmatically created button that it should use a XML resource file, which contains the appearance and layout parameters, instead of setting these parameters programmatically.

我研究了类似命名的问题,并花了一些时间弄乱API本身,但无济于事.

I've looked at similarly named questions and spent time messing with the API itself, to no avail.


这是我正在尝试执行的操作的近似值,希望可以使我的解释更清楚:


Here's an approximation of what I'm trying to do that will hopefully make explanations a bit clearer for me:

private TextView textView;
private SomeObject someObject;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View scrollView = inflater.inflate(R.layout.fragment_play_game, container, false);
    textView = (TextView) scrollView.findViewById(R.id.game_data_text);
    textView.setText(someObject.getTextForTextView());

    LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.game_data_container);
    for (String optionText : someObject.getTextForButtons()) {
        layout.addView(createOptionButton(optionText, layout));
    }
    return scrollView;
}

private View createOptionButton(String optionText, LinearLayout layout) {
    Button optionButton = new Button(this.getActivity());
    // set button layout/options here, somehow??
    optionButton.setText(optionText);
    return optionButton;
}

我的片段XML布局文件如下所示(这是我要向其中添加按钮的LinearLayout):

My XML layout file for the fragment looks like this (It's this LinearLayout that I'm trying to add buttons to):

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/game_data_container"
        etc... >

        <TextView 
           android:id="@+id/game_data_text"
           etc... />

    </LinearLayout>

</ScrollView>

此外,如果我要为按钮创建一个XML布局文件(我们称其为custom_button.xml),它应该看起来像这样吗:

Also, if I'm to create an XML layout file for the button (lets call it custom_button.xml) should it look something like this?:

<?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/play_game_option_button"
        etc... />

更新:
只是为了扩展MrFox @的讨论内容,我要做的就是替换此行:

Update:
Just to expand a bit on what MrFox@ is talking about, what I did to get it working was replace this line:

Button optionButton = new Button(this.getActivity());

与此:

Button optionButton = (Button) inflater.inflate(R.layout.play_game_option_button, layout, false);

...这会膨胀仅包含Button布局(按钮模板)的xml文件.在这种情况下,它将返回该文件的根视图,它只是按钮,因为在文件中按钮上方没有父级.

...which inflates an xml file containing only a Button layout (the button template). In this case, it returns the root view of that file, which is just the button because there's no parent above the button in the file.

但是,如果我将最后一个布尔值(attachToParent)设置为true,它将返回按钮将位于其中的根容器(这只是传递给调用的'layout'变量).

However, if I had have set the last boolean value (attachToParent) to true, it would have returned the root container that the button will be in (which is just the 'layout' variable that was passed into the call).

现在我可以使用该模板制作尽可能多的按钮.

I can now produce as many buttons as I want using this template.

推荐答案

您是否考虑过创建一种布局,该布局只是具有所应用XML样式的按钮,然后将其放大为线性布局?

Have you thought of making a layout that is just the button with the applied XML styles and then inflating it into your linear layout?

类似:

inflater.inflate(R.layout.StyledButton,MyLinearLayout,true);

inflater.inflate(R.layout.StyledButton, MyLinearLayout, true);

这篇关于使用XML布局作为模板以编程方式创建android按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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