【问题标题】:Image Button resizing图片按钮调整大小
【发布时间】:2019-04-10 08:51:50
【问题描述】:

我正在 Android Studio 中设置一个图像按钮。在设计视图中看起来一切正常,但是在加载应用程序时,图像按钮被移动到左上角并减小了大小?

如下图:

设计视图

XML 代码 - 当前



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.PhysicalActivity">

    <ImageButton
        android:id="@+id/btnImageGallery"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:onClick="onImageGalleryClicked"
        android:src="@drawable/placeholder"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.921"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>

体育锻炼代码

package com.example.futura.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.futura.R;

import java.io.File;

public class PhysicalActivity extends AppCompatActivity {

    public static final int IMAGE_GALLERY_REQUEST = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_physical1);

    }

    public void onImageGalleryClicked(View v){


        // invoke the image gallery using an implict intent.
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

        File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String pictureDirectoryPath = pictureDirectory.getPath();

        // finally, get ui represenation

        Uri data = Uri.parse(pictureDirectoryPath);

        // set the data and type, get all image types

        photoPickerIntent.setDataAndType(data, "image/*" );

        startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);


    }

}

预览

有人对纠正这个问题有什么建议吗?

【问题讨论】:

  • intead 使用marginTop,您可以使用垂直偏差。此外,您没有向图像按钮添加任何 src
  • 尝试删除边距或者你可以设置边距0
  • 使用约束时,不要为imageButton提供硬编码的高度和宽度。让它根据它的约束跨越。

标签: java android android-layout imagebutton


【解决方案1】:

尝试添加layout_constraintHorizontal_biaslayout_constraintStart_toStartOf

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/btnImageGallery"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:onClick="onImageGalleryClicked"
        android:src="@drawable/placeholder"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.921"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 感谢您添加此内容。不幸的是,我的原始预览中仍然得到相同的结果。
  • 你如何膨胀activity_physical.xml?请向我们展示您那部分的 java 代码。
  • 什么意思?我使用意图来激活activity_physical。你想看看那个代码吗?
  • 向我们展示您的PhysicalActivity.class。我测试了我的代码,它工作正常
  • 哇哈哈,对不起。感谢您的时间和帮助,我很感激!我会假装那没有发生... ;)
【解决方案2】:

删除边距(开始、顶部、底部、结束)并调整layout_constraintHorizontal_biaslayout_constraintVertical_bias 以获得所需的结果。

<android.support.v7.widget.AppCompatImageButton
    android:id="@+id/startSyncButton"
    android:layout_width="200dp"
    android:layout_height="300dp"
    app:layout_constraintHorizontal_bias="0.9"
    app:layout_constraintVertical_bias="0.7"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

【讨论】:

  • 谢谢你。我复制了您的代码并匹配了我的应用程序 ID。虽然,我似乎仍然得到了与预览图像中显示的结果相同的结果。
  • 忽略之前的评论 - 我删除了边距,并调整了这些值。不幸的是仍然得到相同的结果
  • activity_physical、activity_physical1、activity_physical2、activity_physical3、......怎么样?
  • 你有多少种布局使用这种命名方式?您正在编辑哪个布局?
  • 感谢您的帮助。现在排序,你是绝对正确的。像白痴一样使用错误的布局:)
猜你喜欢
  • 2011-07-04
  • 2013-03-17
  • 1970-01-01
  • 2014-05-02
  • 2022-12-09
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多