1

我有一个背景图片:

android:background="@drawable/bg">

和一个文本视图:

final TextView theTimer = new TextView(this);

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

4

2 回答 2

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);
于 2015-06-02T09:46:16.110 回答
0

创建 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);
于 2015-06-02T09:42:37.957 回答