Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调整布局xml中的imagebutton集的大小_Java_Android_Xml_Layout - Fatal编程技术网

Java 调整布局xml中的imagebutton集的大小

Java 调整布局xml中的imagebutton集的大小,java,android,xml,layout,Java,Android,Xml,Layout,Android开发新手,在Android Studio中,我创建了一个布局,其中包括2-ImageButtons和1-ImageView,它们都排成一行 我制作了所有的ldpi、mdpi、hdpi、xhdpi、xxhdpi图像,并将它们放在文件夹中 在Android Studio中一切看起来都不错,但在模拟器中运行时,图像会更大,并延伸到设备屏幕之外,这并不是我希望看到的规模 因此,我尝试按项目的id调出项目,并根据MainActivity Java文件中的屏幕大小手动设置项目 这是我目前拥有的

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>

但是,这不起作用。。你知道我该如何调整菜单的大小吗?或者有没有其他更自动地为Android开发的每个屏幕缩放图像的想法?

在观看了大量教程视频7天后,我终于获得了eureka

我已经多次更改了xml,所以我将只发布最新的对我有用的内容。但有两个关键项目帮助我克服了这一困难

在XML中添加scaleType=fitCenter 在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>

你能发布布局的全部xml代码吗?谢谢你的提问,我已经更新了,包含了我的整个布局xml。我假设ImageButton嵌套在GridLayout中,GridLayout嵌套在RelativeLayout中。。
<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>
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));