Android中的动态相对布局

发布于 2024-11-03 05:52:45 字数 2249 浏览 0 评论 0原文

我正在尝试将两个图像放在一起。我可以让它与 xml 文件一起正常工作,但我想动态地执行此操作。 ctdeasyone 是一个透明图像。

所以这工作正常......

<?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>

当我这样做时。仅显示第二张图像(它是透明的。)任何专家都可以对此提出建议吗?这里是新手...这是我的第一个问题。 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 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);

    }        

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2024-11-10 05:52:45

我不得不将它放入模拟器中并玩了一会儿,直到我看到它:

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

你永远不会为 iv2 设置图像资源!

我改变了它,现在我看到了预期的两张图像。

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.

一念一轮回 2024-11-10 05:52:45

这非常适合 FrameLayout 。因为您希望图像彼此重叠。

This is perfect for FrameLayout . Since you want the images right on top of each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文