0

我有一个具有以下 xml 的相对布局:

<RelativeLayout
    android:id="@+id/date_bar_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/navigation_bar_layout"
    android:background="@color/light_gray"
    android:gravity="center">

    <TextView
        android:id="@+id/detaildatetextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text=""
        android:textColor="@color/black"
        android:textSize="20dp"
        android:textStyle="bold" />
</RelativeLayout>

如您所见,它已经有一个文本视图。在我的代码中的某个地方,我需要在文本视图之前以编程方式添加图像。这意味着首先显示图像,然后显示文本视图。但是当我执行代码时会显示图像,而文本视图则不会。请帮我解决这个问题。我的实现如下:

image  = new ImageView(getApplicationContext());
image.setId(0);
dateText = (TextView) dateBarLayout.findViewById(R.id.detaildatetextview);
dateText.setText("Dummy text");
RelativeLayout.LayoutParams params = (LayoutParams) dateBarLayout.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, dateText.getId());                       
String icon = "drawable/"+ path;
icon = icon.replaceAll("-", "_");
icon = icon.toLowerCase();
int imageResource = this.getApplicationContext().getResources().getIdentifier(icon, null,this.getApplicationContext().getPackageName());
image.setImageResource(imageResource);
dateBarLayout.addView(image);

我不知道我要去哪里错了。请帮助我摆脱困境。提前致谢...:-)

4

3 回答 3

0

您需要为正在添加的视图设置布局参数,以将其放置在相对布局中,否则它将重叠,布局中的左上视图,因此使用http://developer.android.com代替 addView(View) /reference/android/view/ViewGroup.html#addView(android.view.View, android.view.ViewGroup.LayoutParams)方法将视图添加到布局中。因此,将您的代码更改为以下内容:

image  = new ImageView(getApplicationContext());
    image.setId(0);

    dateText = (TextView) dateBarLayout.findViewById(R.id.detaildatetextview);

    dateText.setText("Dummy text");



        RelativeLayout.LayoutParams params = (LayoutParams) dateBarLayout.getLayoutParams();
    params.addRule(RelativeLayout.LEFT_OF, dateText.getId());

        String icon = "drawable/"+ path;
        icon = icon.replaceAll("-", "_");
            icon = icon.toLowerCase();

        int imageResource = this.getApplicationContext().getResources().getIdentifier(icon, null,this.getApplicationContext().getPackageName());

        image.setImageResource(imageResource);
                    dateBarLayout.addView(image, params);
于 2012-09-13T05:37:56.643 回答
0
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/Rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>



 <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:gravity="left"
    android:onClick="addAddbutton"
    android:text="add"
    android:visibility="visible" />

</RelativeLayout>
</RelativeLayout>


mainActivity.java

package com.example.textboxonclick;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

private RelativeLayout Rl;
private EditText myEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln = (RelativeLayout) findViewById(R.id.Rl);

 }
 public void addAddbutton(View v) {
 myEditText = new EditText(this);
 Rl.addView(myEditText);
}
}
于 2015-04-13T12:22:24.360 回答
0

之后将以下代码添加到您的java文件中setcontentview(R.layout.main);,它将解决您的问题。

ImageView image = new ImageView(this);
RelativeLayout dateBarLayout=(RelativeLayout)findViewById(R.id.date_bar_layout);
TextView dateText = (TextView) dateBarLayout.findViewById(R.id.detaildatetextview);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, dateText.getId());
image.setLayoutParams(params);
image.setImageResource(R.drawable.ic_launcher);
dateBarLayout.addView(image);
于 2012-09-13T06:08:25.273 回答