【问题标题】:Creating LinearLayout in Java - Elements are not shown在 Java 中创建 LinearLayout - 不显示元素
【发布时间】:2011-11-06 15:44:07
【问题描述】:

我正在尝试使用 Java 中的 TextViews 创建一个 LinearLayout,因为元素的数量是动态指定的,因此使用 XML 对我来说不起作用。 这是我的代码的一个小示例:

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    TextView titleView = new TextView(this);
    titleView.setWidth(LayoutParams.WRAP_CONTENT);
    titleView.setHeight(LayoutParams.WRAP_CONTENT);
    titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
    titleView.setText("Hallo Welt!");
    layout.addView(titleView);

    setContentView(layout);

}
}

当我开始这个活动时,它不会显示这个 TextView,但它也不会显示错误。 有人给点建议吗?

【问题讨论】:

    标签: java android view android-linearlayout


    【解决方案1】:

    试试这个,

      LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
            TextView titleView = new TextView(this);
            LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            titleView.setLayoutParams(lparams);
            titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
            titleView.setText("Hallo Welt!");
            layout.addView(titleView);
    
            setContentView(layout);
    

    【讨论】:

    • 干得好!那是错误
    • 已过时,FILL_PARENT 已弃用。
    【解决方案2】:

    试试这个

    titleView.setWidth(100);
    titleView.setHeight(40);
    

       titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    

    【讨论】:

      【解决方案3】:
      TextView titleView = new TextView(this);
          titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
          titleView.setText("Hallo Welt!");
          layout.addView(titleView);
          setContentView(layout);
      

      【讨论】:

        【解决方案4】:

        使用此代码:

            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        
            TextView titleView = new TextView(this);
            titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
            titleView.setText("Hallo Welt!");
            layout.addView(titleView);
        
            setContentView(layout);
        

        【讨论】:

          猜你喜欢
          • 2023-04-02
          • 1970-01-01
          • 2019-11-30
          • 1970-01-01
          • 1970-01-01
          • 2015-12-28
          • 2019-05-15
          • 1970-01-01
          • 2014-06-20
          相关资源
          最近更新 更多