【问题标题】:How can I create a layout with imageviews overlapping?如何创建图像视图重叠的布局?
【发布时间】:2015-05-11 10:30:31
【问题描述】:

我正在尝试动态添加 Imageviews 我想创建这种布局,ImageViews 重叠,为 我的安卓应用程序。

我不知道如何在 java 代码中设置重叠。我想使用 java 代码,因为我正在动态添加 Imageview!

这是我做的代码:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/frame1">
    </RelativeLayout>

这是我的java代码:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.frame1);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    mImageView = new ImageView (this);
    mImageView.setId(1);
    name = "0" + ".jpg";
    Bitmap bitmap = BitmapFactory.decodeFile(url_image+name);
    params1.addRule(RelativeLayout.ALIGN_PARENT_TOP,1);
    params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,1);
    params1.addRule(RelativeLayout.ALIGN_PARENT_START,1);
    params1.addRule(RelativeLayout.ALIGN_TOP,1);
    mImageView.setImageBitmap(bitmap);
    layout.addView(mImageView,params1);
        for (int i = 1;i < num_max; i++){
            mImageView = new ImageView (this);
            mImageView.setId(i+1);
            name = String.valueOf(i) + ".jpg";
            int id = mImageView.getId() - 1 ;
            bitmap = BitmapFactory.decodeFile(url_image+name);
            mImageView.setImageBitmap(bitmap);
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            params2.addRule(RelativeLayout.BELOW,id);
           params2.addRule(RelativeLayout.ALIGN_START,id);
            params2.addRule(RelativeLayout.ALIGN_LEFT,id);
            layout.addView(mImageView,params2);
    }

【问题讨论】:

  • 使用FrameLayout,一种为相同目的而设计的Android布局。
  • @JibranKhan RelativeLayout 在这种情况下的工作方式相同,我认为问题不在于布局

标签: java android layout imageview


【解决方案1】:

如果你想图像相互重叠(正如我从你的问题中理解的那样)你应该删除这一行:

 params2.addRule(RelativeLayout.BELOW,id);

如果您想要其他内容,请随时在此答案下方发表评论

【讨论】:

    【解决方案2】:

    使用相对布局 使用此代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/iv"
         />
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      以下是如何实现它的示例。

      MainActivity.Java

      public class MainActivity extends Activity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
      
                  RelativeLayout layout = (RelativeLayout)               findViewById(R.id.frame1);
      
      
                  //Add first image view
                  ImageView mImageView = new ImageView (this);
                  mImageView.setId(1);
                  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.color1);
                  RelativeLayout.LayoutParams params1 = new       RelativeLayout.LayoutParams(300, 300);
                  mImageView.setImageBitmap(bitmap);
                  layout.addView(mImageView,params1);
      
      
                  //Add second image view
                  ImageView mImageView2 = new ImageView (this);
                  mImageView2.setId(2);
                  Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.color2);
                  mImageView2.setImageBitmap(bitmap2);
                  RelativeLayout.LayoutParams params2 = new       RelativeLayout.LayoutParams(300,300);
                  params2.setMargins(150, 150, 0, 0);
                  layout.addView(mImageView2,params2);
      
              }
          }
      

      activity_main.xml

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.example.sample.MainActivity" >
      
           <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:id="@+id/frame1">
          </RelativeLayout>
      
      </RelativeLayout>
      

      输出截图:

      所需图片:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 2019-11-25
        • 2021-12-26
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多