本文介绍了在Android的动态相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对彼此的TP两个图像。我可以把它正常工作与XML文件,但我想动态地做到这一点。 ctdeasyone是透明的形象。

所以这个工作得很好。

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    < ImageView的
        机器人:ID =@ + ID / BCK1
        机器人:SRC =@绘制/ fish2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:scaleType =fitXY
        机器人:layout_gravity =中心>
    < / ImageView的>    < ImageView的
        机器人:ID =@ + ID / bck2
        机器人:SRC =@绘制/ ctdeasyone
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:scaleType =fitXY
        机器人:layout_gravity =中心>
    < / ImageView的>< / RelativeLayout的>

当我这样做。只有第二图像显示出来(它是透明的。)可以在任何关于这方面的专家的意见吗? Newbbie这里...这是我的第一个问题。 TIA。

 公共类TwoPicksOnEachOther延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //拼抢应用程序上下文
        最后上下文的背景下= getApplication();        的RelativeLayout的RelativeLayout =新的RelativeLayout(本);        最终的ImageView IV =新ImageView的(本);
        iv.setImageResource(R.drawable.fish2);
        RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(IV,LP);        //创建透明图像
        最终的ImageView IV2 =新ImageView的(本);
        iv.setImageResource(R.drawable.ctdeasytwo);
        RelativeLayout.LayoutParams LP2 =新RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(IV2,LP2);
        的setContentView(RelativeLayout的);    }}


解决方案

我不得不把它的仿真器和它玩了一段时间,直到我看到了它:

  iv.setImageResource(R.drawable.fish2);
(......)
iv.setImageResource(R.drawable.ctdeasytwo);

你永远设置图像资源,IV2!

我改变了这一切,现在我看到两个影像预期。

I am trying to have two images on tp of each other. I can have it work fine with an xml file but I would like to do this dynamically. ctdeasyone is a transparent image.

So this works fine..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/bck1"
        android:src="@drawable/fish2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_gravity="center">
    </ImageView>

    <ImageView
        android:id="@+id/bck2"
        android:src="@drawable/ctdeasyone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_gravity="center">
    </ImageView>

</RelativeLayout>

When I do this. only the second image shows up (it is the transparent one.) Can any of the experts advice on this? Newbbie here... This is my first question. TIA.

public class TwoPicksOnEachOther extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Grabbing the Application context
        final Context context = getApplication();

        RelativeLayout relativeLayout = new RelativeLayout(this);

        final ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.fish2);


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv,lp);

        // Creating transparent image
        final ImageView iv2 = new ImageView(this);
        iv.setImageResource(R.drawable.ctdeasytwo);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        relativeLayout.addView(iv2,lp2);
        setContentView(relativeLayout);

    }

}
解决方案

I had to put it in the emulator and play with it for a while until I saw it:

iv.setImageResource(R.drawable.fish2);
(...)
iv.setImageResource(R.drawable.ctdeasytwo);

You're never setting the image resource for iv2!

I changed that and now I see two images as expected.

这篇关于在Android的动态相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:50