好的,实际上我不明白,为什么你使用TextView
类作为你的内容视图。在这种情况下,你当然可以让你自己的 textview 扩展默认的一个,并为它创建带有 imageview 的特定布局,但是,最简单(也更合乎逻辑)的方法是使用 and 为你的活动创建 lauoutTextView
文件ImageView
。
例如,它可以是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
您的活动 onCreate 方法将类似于:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(textView);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setTextSize(40);
textView.setText(message);
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
}
您需要在通过 id 查找视图之前调用 setContentView。
如果您必须从 java 中创建 texview 动态(我从您的代码中看不到任何原因),您可以将id属性添加到LinearLayout
并通过 id 找到它并将 texview 添加到其中,它将放置在 imageview 之后。
ps实际上有很多方法可以做到这一点,如果我的回答不适合你,请更准确地定义你的问题
pps 如果您确实必须在运行时创建 textView 并且不能使用 xml 布局,则可以LinearLayout
在运行时创建(LinearLayout layout = new LinearLayout(this)
然后创建 textView 和 ImageView 并将这两个视图添加到该布局中,然后将此布局设置为内容查看活动