以编程方式将项添加到相对布局

时间:2012-09-13 05:26:52

标签: android android-layout relativelayout

我的相对布局有以下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>

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

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);

我不知道我哪里出错了..请帮我摆脱这个。在此先感谢... :-)

3 个答案:

答案 0 :(得分:0)

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

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);

答案 1 :(得分:0)

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

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);

答案 2 :(得分: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);
}
}