Несколько просмотров в Android
У меня есть фоновое изображение:
android:background="@drawable/bg">
и TextView:
final TextView theTimer = new TextView(this);
Если я использую setContentView, я могу разместить только один из них на экране. Как мне совместить их обоих в одном упражнении?
2 ответа
Решение
Сделай что-то вроде этого -
файл 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);
Создайте 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);