【问题标题】:Multiple views in Android [duplicate]Android中的多个视图[重复]
【发布时间】:2015-06-02 09:37:21
【问题描述】:

我有一张背景图片:

android:background="@drawable/bg">

和一个文本视图:

final TextView theTimer = new TextView(this);

如果我使用 setContentView,我只能将其中一个放在屏幕上。 我怎样才能将它们都放在一项活动中?

【问题讨论】:

    标签: java android background-image textview setcontentview


    【解决方案1】:

    做这样的事情-

    my_layout.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" 
            android:background="@drawable/bg">
        </LinearLayout>
    

    活动代码

    setContentView(R.layout.my_layout);
    final TextView theTimer = new TextView(this);
    LinearLayout ll = (LinearLayout)findViewByID(R.id.ll);
    ll.addView(theTimer);
    

    【讨论】:

    • 或者更好的是,将 textview 也放入 xml 中。
    • 然后在 xml 文件中创建 TextView 以及为什么要以编程方式添加它。
    • 我只是想以编程方式添加它。它对性能有任何负面影响吗?
    【解决方案2】:

    创建 RelativeLayout创建ImageView

      RelativeLayout layout = new RelativeLayout(this);
        ImageView img = new ImageView(this);
        img.setImageResource(R.drawable.your_drawable);
    
    
        TextView tv1 = new TextView(this);
        tv1.setText("B");
        RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        tv1.setLayoutParams(layoutParams);
    
        layout.addView(img);
        layout.addView(tv1);
    
        setContentView(layout);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2011-06-22
      • 1970-01-01
      • 2013-06-14
      • 2013-01-27
      • 2023-04-02
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多