【问题标题】:Why would my dynamically generated content not show up in tablelayout为什么我的动态生成的内容不会显示在表格布局中
【发布时间】:2013-02-18 20:29:11
【问题描述】:

我正在尝试将一些按钮加载到片段内的表格布局中。这怎么不出现。我看到改变背景是有效的(在 onCreateView(......) 中从灰色变为红色)但是当我调用 loadAllButtons 时它们没有显示出来。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
        if(container == null)
            return null;
        basedView = (TableLayout) inflater.inflate(R.layout.fragment_difficult_sound_syllable, container, false);
        currentSyllable = getArguments().getString(StaticValues.DIFFICULT_SOUND_SELECTOR);

        basedView.setBackgroundColor(Color.RED);
        return basedView;
}

下面是我的 loadAllButtons,在有人问是之前,它说所有内容都已加载(例如,添加的按钮和行,在 row.add() 和 basedView.add() 之后通过 logcat 检查)

private void loadAllButtons()
{

    DataBaseHelper tmpHelper = new DataBaseHelper(getActivity().getApplicationContext());
    ArrayList<ButtonInfoContainer> tmpArray = null;


    if(currentSyllable.contentEquals(StaticValues.CONSONANT))
    {
        tmpArray = tmpHelper.returnAllConsonants();
        Log.i(StaticValues.TAG, "returned all consonants");
    }
    else
    {
        tmpArray = tmpHelper.returnAllVowels();

        Log.i(StaticValues.TAG, "returned all vowels");
    }

    if(tmpArray != null)
    {
        TableRow row = null;
        InfoButton tmpButton = null;

        //LayoutParams rowParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //TableRow.LayoutParams equalParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0); 
        LinearLayout.LayoutParams buttonLayout = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        if(basedView != null)
        {

            for(int i = 0; i < tmpArray.size(); i++)
            {

                //adds new row to table if modulus is 0
                if(( i % 2 ) == 0)
                {
                    row = (TableRow) getLayoutInflater(getArguments()).inflate(R.layout.table_row, null);
                    //row.setLayoutParams(rowParams);
                    basedView.addView(row);
                }

                tmpButton = (InfoButton) getLayoutInflater(getArguments()).inflate(R.layout.info_button, null);
                tmpButton.setBackgroundResource(R.drawable.button_bg);
                tmpButton.setLayoutParams(buttonLayout);

                //set button Text
                tmpButton.setText(tmpArray.get(i).buttonText);
                //Obtain extra held info
                tmpButton.setExtraInfo(tmpArray.get(i).infoHeld);

                //tmpButton.setWidth(buttonWidth);
                //tmpButton.setHeight(buttonHeight);

                //tmpButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
                tmpButton.setTextColor(getResources().getColor(R.color.button_text_color));
                tmpButton.setTypeface(Typeface.DEFAULT_BOLD);

                row.addView(tmpButton);
                tmpButton.setOnClickListener(listenerDynamicButton);
            }

        }
    }
}

出于某种原因,即使我的 logcat 输出显示一切都很好(按钮显示正确名称的按钮数量),但表格布局没有被填充。

【问题讨论】:

    标签: android dynamic android-tabhost fragment android-tablelayout


    【解决方案1】:

    就我自己的问题找出答案。

    之后的一切
    tmpButton = (InfoButton) getLayoutInflter()...
    

    row.addView(tmpButton);
    

    必须删除,除非

    tmpButton.setText();
    

    tmpButton.setExtraInfo(..);
    

    虽然我不知道为什么那些杀了我的布局。因此,如果有人想出那个部分,我会很乐意接受这个答案。

    【讨论】:

    • 看来你的问题是 LayoutParams。
    • 可能是的。这是以前活动的复制粘贴代码。它在那里工作,但在片段中没有工作。很奇怪。
    猜你喜欢
    • 2015-01-17
    • 2011-07-12
    • 1970-01-01
    • 2011-07-08
    • 2019-07-18
    • 2017-09-28
    • 2020-06-03
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多