android:imageview顶部的多个屏幕大小的按钮
您好,我正在android上开发一个应用程序,我有一些布局,我希望在横向模式下有一个填充android:imageview顶部的多个屏幕大小的按钮,android,image,button,size,screen,Android,Image,Button,Size,Screen,您好,我正在android上开发一个应用程序,我有一些布局,我希望在横向模式下有一个填充屏幕宽度的图像。最重要的是,我想在特定的地方有一些按钮 我使用了一个滚动视图(因为如果图像填满了屏幕的宽度,那么它的高度就不合适了),其中有一个相对的布局 在这个相对布局中,我放置了imageview和按钮。图像将填充宽度,并将内容包裹到高度。通过设置相对布局中的左页边距和上页边距来放置按钮 在我测试过的手机上,一切看起来都很好,但当我在不同的设备上测试时(其他屏幕大小和密度),所有的按钮都放错了位置,不再足
屏幕宽度的图像。最重要的是,我想在特定的地方有一些按钮
我使用了一个滚动视图
(因为如果图像填满了屏幕的宽度,那么它的高度就不合适了),其中有一个相对的布局
在这个相对布局中,我放置了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>
我已经做了你想做的事。不幸的是,若您想要精确控制任何屏幕大小上的定位,以及精确的纵横比,那个么您就必须以编程方式定位资源。如果您使用xml,您可以非常接近,但不会有精确的定位控制。对于初学者来说,以编程方式定位可能会很乏味。因为你只需要在这里和那里放置几个按钮,所以这应该不会太麻烦
我是如何做到的:以编程方式获取屏幕宽度和屏幕高度。然后计算按钮的纵横比。接下来,计算按钮相对于屏幕宽度和屏幕高度的位置(单位为%)。有了所有这些信息,你应该能够准确地将按钮定位在你想要的位置,在任何屏幕大小和特定方向上。我建议将所有其他内容都放在xml上,并且只以编程方式执行按钮。在方向更改时,您需要重新计算所有内容,这可能会很烦人。我建议您坚持使用1个方向,以后可能支持多个方向。我使用这些辅助功能根据屏幕高度和宽度的百分比来移动按钮。首先,我在图形布局中为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();
}
我希望它能帮助一些人。当涉及到不同的屏幕密度时,RelativeLayout非常棘手,您必须以编程方式根据屏幕大小设置它们的位置和大小!我确实在考虑这个问题,但不知道如何在代码中获取和设置按钮属性。布局放置在带有片段寻呼机适配器的viewpager中。我必须用xml绘制布局,调整按钮(在哪里?)并重新绘制布局,还是必须在代码中添加按钮?在代码中添加按钮。您可以使用findViewById获取对viewpager的引用,并使用此引用向其添加按钮。谢谢!我要试试看。