how to place an image over another one on android app?

后端 未结 4 1485
予麋鹿
予麋鹿 2020-12-17 04:47

how i can put im2 in the correct place

FrameLayout rv =(FrameLayout)findViewById(R.id.my_ph);


    ImageView im1 = new ImageView(this);
    im1.setBackgrou         


        
相关标签:
4条回答
  • 2020-12-17 04:59

    Instead of using android:src in the imageView xml use android:background then just recieve it at the source java file then use the following code:

    ImageView img=(ImageView)findViewById(R.id.image);
    img.setBackgroundDrawable(getResource().getDrawable(R.drawable.lamp_on));
    

    Hope this will work for you.

    0 讨论(0)
  • 2020-12-17 05:12

    Use this make this xml in your drawble folder, you can add more images to it in the same way. they will all apear on top of each other.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <item>
        <bitmap android:src="@drawable/img1"
            android:gravity="center"
            />
    </item>
    <item>
        <bitmap android:src="@drawable/img2"
            android:gravity="center"
            />
    </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-12-17 05:13

    In layout add one more ImageView and align Top, Bottom, Right, Left to first one. Make it invisible;

    <FrameLayout android:id="@+id/my_ph"    
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView  
        android:id="@+id/image"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/sketch" android:layout_alignParentTop="true"/>
    <ImageView  
        android:id="@+id/image2"
        android:layout_width="fill_parent" 
        android:layout_height="fill_paren" 
        android:layout_alignTop="@id/image"
        android:layout_alignLeft="@id/image"
        android:layout_alignRight="@id/image"
        android:layout_alignBottomp="@id/image"
        android:visibility="INVISIBLE"/>
    </FrameLayout>
    

    Then in code:

    ImageView image2 =(ImageView)findViewById(R.id.image2);
    image2.setVisibility(View.VISIBLE);
    image2.setImageResource(R.drawable.my_image);
    
    0 讨论(0)
  • 2020-12-17 05:13

    and the answer....

     RelativeLayout rv = (RelativeLayout) findViewById(R.id.my_ph);
     RelativeLayout.LayoutParams params;
     ImageButton im1 = new ImageButton(this);
    
     im1.setBackgroundResource(R.drawable.lamp);
     im1.setId(i);
     im1.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            TextView tx = (TextView) findViewById(R.id.textView1);
            tx.setText("lamp #" + v.getId());
         }
     });
    
     params = new RelativeLayout.LayoutParams(40, 40);
     params.leftMargin = x;
     params.topMargin = y;
     rv.addView(im1, params);
    

    XML Layout:

     <RelativeLayout 
                android:id="@+id/my_ph"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:gravity="bottom">
        <ImageView 
                android:id="@+id/image" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_alignParentTop="true"
                android:background="@drawable/map" />
        <TextView 
                android:id="@+id/textView1" 
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_below="@+id/image" 
                android:layout_alignParentLeft="true">
        </TextView>
    
     </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题