-2

我需要将 ImageView 放在使用 java 构造的 TextView 下。textview 正在显示来自先前活动的信息。这是我的代码:

package com.example.a_simple_ui;
public class MainActivity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    Intent recieve = getIntent();
    String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);
    setContentView(textView);
}
}

所以现在我需要在上面的textview下放一张图片并改变背景颜色。通过 java 或 XML。谢谢你。

4

2 回答 2

2

您可以使用以下两个建议中的任何方法:

1)如果您的布局设计是固定的,最好使用基于 xml 的布局(静态布局)而不是添加布局运行时。

为此,首先创建 xml 布局 main_Activity.xml,如:

main_Activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <Imageview
            android:id="@+id/ivIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

</LinearLayout>

MainActivity2 .java

包 com.example.a_simple_ui;公共类 MainActivity2 扩展 Activity {

@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.tvDesc);
textView.setTextSize(40);
textView.setText(message); } }

2)您需要在线性布局中添加 TextView 和 Imageview 然后需要在 setContentView() 中设置该线性布局,例如:

    package com.example.a_simple_ui;
    public class MainActivity2 extends Activity {

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

        Intent recieve = getIntent();
        String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        imageView.setImageResource(R.drawable.ic_launcher);

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

        layout.addView(textview);
        layout.addView(imageView);

        setContentView(layout);
    }
    }
于 2013-10-26T20:18:19.623 回答
0

好的,实际上我不明白,为什么你使用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 并将这两个视图添加到该布局中,然后将此布局设置为内容查看活动

于 2013-10-26T20:06:45.100 回答