繁体   English   中英

调整布局xml android中设置的imagebutton的大小

[英]resizing imagebutton set in layout xml android

我是Android开发的新手,在Android Studio中,我创建了一个包含2-ImageButtons和1-ImageView的布局,它们都排成一行。

我已经制作了所有ldpi,mdpi,hdpi,xhdpi,xxhdpi图像,并将它们放在它们的文件夹中。

一切在Android Studio中看起来都不错,但是当在模拟器中运行时,图像会更大,并且会超出设备屏幕,而这并不是我想要看到的比例。

因此,我尝试通过ID调用项目,并根据MainActivity Java文件中的屏幕大小手动设置它们。

这是整个布局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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/mainView" tools:context=".MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/backgroundView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@mipmap/yellow_background" />

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/gridLayout">

    <ImageButton
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:id="@+id/menuBtn"
        android:layout_row="0"
        android:layout_column="0"
        android:background="@mipmap/menu_button"
        android:layout_marginTop="65dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="30dp" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="81dp"
        android:id="@+id/logo"
        android:layout_row="0"
        android:layout_column="1"
        android:background="@mipmap/logo_image"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:id="@+id/settingsBtn"
        android:layout_row="0"
        android:layout_column="2"
        android:background="@mipmap/settings_button"
        android:layout_marginTop="65dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="30dp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="345dp"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_margin="20dp"
        android:layout_gravity="fill_horizontal|center_horizontal">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="view 1"/>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="view 2"/>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="view 3"/>

        </android.support.v4.view.ViewPager>
    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rights Reserved"
        android:id="@+id/textView2"
        android:layout_row="2"
        android:layout_column="0"
        android:layout_gravity="center_horizontal"
        android:layout_columnSpan="3" />

</GridLayout>

在Java文件中,我试图像这样手动调整大小:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    double topMenuWidth = 0.1;

    ImageButton menuBtn = (ImageButton)findViewById(R.id.menuBtn);
    menuBtn.setMaxWidth((int)(width * topMenuWidth));

但是,这是行不通的。任何想法都可以像我正在尝试的那样如何调整menuBtn的大小? 还是有其他想法可以为Android开发的每个屏幕提供一种更为自动的缩放图像的方式?

经过7天的观看大量教程视频并将我的头撞在墙上,我终于获得了尤里卡!

我已经多次更改了xml,因此我将只发布对我有用的最新版本。 但是有2个关键项目帮助我克服了这一困难。

  1. 在XML中添加“ scaleType =” fitCenter“”
  2. 在Java中使用“ settingLayoutParams”

在XML中:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/headerLayout">
    <ImageButton
        android:id="@+id/menuBtn"
        android:contentDescription="@string/menu_button"
        android:src="@mipmap/menu_button"
        android:background="#00000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:scaleType="fitCenter" />
</RelativeLayout>

在Java中:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

height = metrics.heightPixels;
width = metrics.widthPixels;

double topMenuWidthScale = 0.1;
int topMenuWidth = (int)(topMenuWidthScale * width);

menuBtn = (ImageButton)findViewById(R.id.menuBtn);

menuBtn.setLayoutParams(new RelativeLayout.LayoutParams(topMenuWidth, topMenuWidth));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM