【问题标题】:Android RelativeLayout Background Picture from Gallery来自图库的 Android RelativeLayout 背景图片
【发布时间】:2013-11-29 10:18:21
【问题描述】:

我在为RelativeLayout 设置背景图像时遇到问题。细节问题: - 我的用户界面中有RelativeLayoutButton。当按钮点击然后显示画廊的意图。从该图库中选择 1 张图片,我想将该图片设置为 RelativeLayout 背景。

我可以调用图库的意图,但我仍然无法将该图片设置为 RelativeLayout 背景。请帮帮我

编辑(这是我从图库意图设置 ImageView 的示例代码):

final ImageView img = (ImageView) findViewById(R.id.img);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
            //img.setImageResource(Intent.);
            Drawable dr = new BitmapDrawable(SELECT_PICTURE);
        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    //Integer selectedImageUri;
    //Integer[] bg = {selectedImageUri};
    if (resultCode == RESULT_OK){
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
                      ImageView img = (ImageView) findViewById(R.id.img);
            img.setImageURI(selectedImageUri);

那么如何将该图片设置为不是ImageView的图像,而是作为RelativeLayout背景

【问题讨论】:

  • 请粘贴您的代码

标签: android android-layout android-gallery


【解决方案1】:

从图库中挑选图片后,您可以这样设置图片-

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            Uri imageUri = data.getData();
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

            RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
            Drawable dr = new BitmapDrawable(bitmap);
            relative.setBackgroundDrawable(dr);
        }
    }

【讨论】:

  • 如果我使用你的代码,我可以把画廊里的图片放在哪里?我不太了解位图:D
【解决方案2】:

试试这个:

<ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/background" />

【讨论】:

    【解决方案3】:

    如果您正在获取可绘制资源,那么您可以使用该可绘制资源设置相对布局的背景,如下所示。

      RelativeLayout relativeLayout;
      relativeLayout= (RelativeLayout) findViewById(R.id.layout1);
      RelativeLayout.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
    

    希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多