如何在android中的片段中动态添加元素 [英] How to add elements dynamically in fragment in android

查看:67
本文介绍了如何在android中的片段中动态添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有片段,我想在点击浮动操作按钮时动态添加元素(textview,按钮)。



我尝试过:



代码:

<前lang = java> @覆盖
public 查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
// 为此片段的布局充气
View view = inflater.inflate(R.layout.xyz,container,false);

FloatingActionButton fab =(FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener( new View.OnClickListener(){
@ Override
public void onClick(查看视图){

// 点击的新元素



// 点击新元素

}
});
返回视图;
}





感谢高级:)

解决方案

下面的示例添加了一个新的LinearLayout,然后在现有的LinearLayout中添加了两个TextView对象。

  private   void  ShowDetail(){
final int [] idList = {R.string.lblOne,R.string.lblTwo,R.string.lblThree,R.string.lblFour};
LinearLayout llv =(LinearLayout)findViewById(R.id.llvDetail);
setTitle( item.toString());
for int id:idList){
// 创建LinearLayout
LinearLayout ll = new LinearLayout( );
ll.setOrientation(LinearLayout.HORIZONTAL);

// 为项目标签创建TextView
TextView txtLabel = new TextView( this );
txtLabel.setText(getString(id)+ );
txtLabel.setWidth(( int )getResources()。getDimension(R.dimen.activity_label_width));
txtLabel.setTextAppearance(getApplicationContext(),android.R.attr.textAppearanceMedium);
// txtLabel.setTextSize(TypedValue.COMPLEX_UNIT_PT,8);
ll .addView(txtLabel);

// 为项目内容创建TextView
TextView txtContent = new TextView( this );
txtContent.setText(getString(id));
txtContent.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8 );
// txtLabel.setTextAppearance(getApplicationContext(),android.R.attr.textAppearanceMedium);
ll.addView(txtContent);
// 添加按钮到XML中定义的LinearLayout
llv.addView(ll );
}


I Have a Fragment, and i want to add elements (textview, button) dynamically when i click on Floating Action Button.

What I have tried:

Code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.xyz, container, false);

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // new elements on click



                // new elements on click

            }
        });
        return view;
    }



Thanks in Advanced:)

解决方案

The following sample adds a new LinearLayout and then adds two TextView objects to that, all within an existing LinearLayout.

private void ShowDetail() {
    final int[] idList = { R.string.lblOne, R.string.lblTwo, R.string.lblThree, R.string.lblFour }; 
    LinearLayout llv = (LinearLayout)findViewById(R.id.llvDetail);
    setTitle("item.toString()");
    for (int id : idList) {
    // Create LinearLayout
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    
    // Create TextView for the item label
    TextView txtLabel = new TextView(this);
    txtLabel.setText(getString(id) + ": ");
    txtLabel.setWidth((int)getResources().getDimension(R.dimen.activity_label_width));
    txtLabel.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
    //		txtLabel.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
    ll.addView(txtLabel);
    
    // Create TextView for the item content
    TextView txtContent = new TextView(this);
    txtContent.setText(getString(id));
    txtContent.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
    //		txtLabel.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
    ll.addView(txtContent);
    //Add button to LinearLayout defined in XML
    llv.addView(ll);
}


这篇关于如何在android中的片段中动态添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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