Android Studio - ImageView下面的TextView

时间:2015-04-05 12:40:55

标签: java android textview imageview

我正在使用Android Studio,我尝试使用Java将TextView放在ImageView下面。为此,我写了这段代码:

   //RelativeLayout
    RelativeLayout relativeLayout = new RelativeLayout(this);
    relativeLayout.setBackgroundColor(Color.parseColor("#006699"));

    //Texto: Press to start
    TextView start = new TextView(this);
    start.setId(R.id.startText);
    start.setText("Press to start");
    start.setTextColor(Color.parseColor("#FFFFFF"));
    start.setTextSize(16);

   //ImageView

    ImageView logo = new ImageView(this);
    int id = getResources().getIdentifier("logo", "drawable", getPackageName());
    logo.setImageResource(id);
    logo.setId(R.id.logo);

    //Posición de la imagen

    RelativeLayout.LayoutParams imageDetails = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    imageDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
    imageDetails.addRule(RelativeLayout.CENTER_VERTICAL);

    relativeLayout.addView(logo, imageDetails);

    //Text Position:

    RelativeLayout.LayoutParams textDetails = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    textDetails.addRule(RelativeLayout.ABOVE, logo.getId());
    textDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

    // start.bringToFront(); It still doesn't appear

    relativeLayout.addView(start, textDetails);

    setContentView(relativeLayout);

但是当我运行该代码时,它只出现在屏幕上的ImageView:

Picture

问题是什么?非常感谢!

3 个答案:

答案 0 :(得分:0)

使用LinearLayout的最简单方法,然后先添加ImageView,然后添加TextView。这将有效。

答案 1 :(得分:0)

1>在Android应用程序中,您需要为您的组件(例如textview,imageview等)提供唯一的id(例如,在layout / main.XML等地方,id = textview1之类)它在你的XML文件中。 您必须将(id)用于您的Java代码以连接后端和forntend。 然后只有你的textview工作。

2>在其他情况下,只需检查您的布局,其他组件没有隐藏您的textview。

答案 2 :(得分:0)

有一个更好的解决方案,只需使用:

start.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.logo);

您不需要其他包装视图,例如RelativeLayout

相关问题