【问题标题】:android: buttons on top of imageview in multiple screen sizesandroid:多个屏幕尺寸的imageview顶部的按钮
【发布时间】:2013-01-14 10:28:53
【问题描述】:

您好,我正在 Android 上开发一个应用程序,我有一些布局,我希望在横向模式下有一个填充 screen-width 的图像。最重要的是,我想要一些特定位置的按钮。

我使用了scrollview(因为如果图像填满了屏幕的宽度,则其高度不适合),其中包含相对布局。

在那个相对布局中,我放置了imageview 和按钮。图像填充宽度并包装高度的内容。按钮是通过设置相对布局中的左边距和上边距来放置的。

在我测试过的手机上一切看起来都不错,但是当我在不同的设备(其他屏幕尺寸和密度)上测试时,所有按钮都在错误的位置并且不再足够大。

我怎样才能改变这一点,使所有比例保持不变?与图片相比,按钮的位置必须完全相同,大小必须相同。

我的代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/svCentralAmerica"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <RelativeLayout
        android:id="@+id/rlCentralAmerica"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ivCentralAmerica"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:src="@drawable/mapcentralamerica" />

        <Button
            android:id="@+id/mexu"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="33dp"
            android:background="@drawable/usa_y"
            android:onClick="onCountryClick"
            android:paddingTop="3dp"
            android:scaleType="fitCenter"
            android:tag="1;U"
            android:text="2"
            android:textColor="@color/Black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/mexr"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignTop="@id/mexu"
            android:layout_marginLeft="4dp"
            android:layout_toRightOf="@id/mexu"
            android:background="@drawable/russia_n"
            android:onClick="onCountryClick"
            android:paddingTop="18dp"
            android:scaleType="fitCenter"
            android:tag="1;R"
            android:text="3"
            android:textColor="@color/Red"
            android:textSize="15sp"
            android:textStyle="bold" />

    </RelativeLayout>

</ScrollView>

【问题讨论】:

  • 当涉及到不同的屏幕密度时,RelativeLayout 非常棘手,您必须以编程方式根据屏幕尺寸设置它们的位置和尺寸

标签: android image button size screen


【解决方案1】:

我实际上已经完成了您的想法。不幸的是,如果您想要在任何屏幕尺寸上精确控制定位以及精确的纵横比,那么您将不得不以编程方式定位资源。如果你使用xml,你可以非常接近,但不会有精确的定位控制。对于初学者来说,以编程方式定位可能很乏味。由于您只是在这里和那里放置几个按钮,因此应该不会太麻烦。

我是如何做到的:以编程方式获取屏幕宽度和屏幕高度。然后计算按钮的纵横比。接下来,计算按钮相对于屏幕宽度和屏幕高度的位置(以 % 为单位)。有了所有这些信息,您应该能够在具有特定方向的任何屏幕尺寸上将按钮准确定位在您想要的位置。我建议将其他所有内容都放在 xml 上,并且只以编程方式执行按钮。在方向改变时,您将需要重新计算所有内容,这可能很烦人。我建议您坚持使用 1 个方向,以后可能会支持多个方向。

【讨论】:

  • 谢谢!我实际上正在考虑这一点,但不知道如何以及在哪里获取和设置代码中的按钮属性。布局放置在带有片段分页器适配器的视图分页器中。我必须用 xml 绘制布局,调整按钮(在哪里?)并重新绘制布局,还是必须在代码中添加按钮?
  • 在代码中添加按钮。您可以使用 findViewById 获取对 viewpager 的引用,并使用此引用将按钮添加到其中。
  • 谢谢!我要试试。
【解决方案2】:

我使用这些辅助函数根据屏幕高度和宽度的百分比来移动我的按钮。首先,我在图形布局中为 320 x 480 屏幕布局屏幕以创建 xml,然后在我的 onCreate 方法中移动按钮。

这是我使用的导入:

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.content.Context;
import android.util.Log;

以下是在 onCreate 中移动按钮的调用:

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

    moveBtn(0.1843,0.1187,0.128,0.7375,R.id.credits_button);
    moveBtn(0.1843,0.1187,0.3125,0.7375,R.id.website_button);
    moveBtn(0.1843,0.1187,0.5,0.7375,R.id.facebook_button);
    moveBtn(0.1843,0.1187,0.6906,0.7375,R.id.support_button);
}

这里是辅助函数:

//----------------------------------------------------------------------------------------------
//  public void moveBtn(double percent_width, double percent_height, double percent_x, double  percent_y, int myObject)
//----------------------------------------------------------------------------------------------
public void moveBtn(double percent_width, double percent_height, double percent_x, double  percent_y, int myObject) {
    int width = (int)Math.round((ScWth(this) * percent_width));
    int height = (int)Math.round((ScHgt(this) * percent_height));
    int x = (int)Math.round((ScWth(this) * percent_x));
    int y = (int)Math.round((ScHgt(this) * percent_y));

     Log.d(TAG, "Object: " + myObject + "/Width: " + width + "/Height: "+ height + "/x: "+ x + "/y: "+ y);

    AbsoluteLayout.LayoutParams myParam = new AbsoluteLayout.LayoutParams(width, height, x, y); 

    Button thisButton = (Button)findViewById(myObject);
    thisButton.setLayoutParams(myParam);

}

//----------------------------------------------------------------------------------------------
//  public static double ScWth(Context context)
//----------------------------------------------------------------------------------------------
    //return the screen width of the device
public static double ScWth(Context context){
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    return display.getWidth();
}

//----------------------------------------------------------------------------------------------
//  public static double ScHgt(Context context)
//----------------------------------------------------------------------------------------------
    //return the screen height of the device
public static double ScHgt(Context context){
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    return display.getHeight();
} 

我希望它对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2011-11-12
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 2023-04-05
    • 2022-07-14
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多