适用于大屏幕和普通屏幕的Android Studio中的布局问题

时间:2016-08-14 23:28:33

标签: java android android-studio layout screen

在我的res文件夹中,我在布局文件夹中有:activity_main.xml(默认为),layout-large文件夹和layout-normal文件夹中的相同文件。

我在Samsung Galaxy S5的预览中设置了我想要的布局(所以大屏幕)然后我运行AVD:这里的布局是不同的。这里的图像定位不同。相反,当我在正常屏幕上运行应用程序时,这不会发生。

Here's the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<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"

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/sfondo"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:scaleType="fitXY"
    android:layout_marginTop="-150dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_centerHorizontal="true"
    android:src="@drawable/tronco1"
    android:layout_alignTop="@+id/imageView" />

<ImageView
    android:layout_width="370dp"
    android:layout_height="230dp"
    android:id="@+id/imageView3"
    android:src="@drawable/titolo"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:id="@+id/imageButton"
    android:src="@drawable/play_h"
    android:scaleType="fitCenter"
    android:background="#00ffffff"
    android:layout_marginTop="40dp"
    android:layout_below="@+id/imageView3"
    android:layout_centerHorizontal="true" />

<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:id="@+id/imageButton3"
    android:src="@drawable/options_h"
    android:scaleType="fitCenter"
    android:background="#00ffffff"
    android:layout_marginTop="16dp"
    android:layout_below="@+id/imageButton2"
    android:layout_alignLeft="@+id/imageButton"
    android:layout_alignStart="@+id/imageButton" />

<ImageButton
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:id="@+id/imageButton2"
    android:src="@drawable/score_h"
    android:scaleType="fitCenter"
    android:background="#00ffffff"
    android:layout_marginTop="17dp"
    android:layout_below="@+id/imageButton"
    android:layout_alignLeft="@+id/imageButton"
    android:layout_alignStart="@+id/imageButton" />

<ImageButton
    android:layout_width="160dp"
    android:layout_height="40dp"
    android:id="@+id/imageButton4"
    android:src="@drawable/removeads_h"
    android:scaleType="fitCenter"
    android:background="#00ffffff"
    android:layout_below="@+id/imageButton3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp" />

1 个答案:

答案 0 :(得分:0)

你的java文件中的

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowmanager = (WindowManager)  getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);


int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;

FrameLayout.LayoutParams frm = (FrameLayout.LayoutParams)findViewById(R.id.trump_center_frame).getLayoutParams();


 frm.width = get_screen_width(710);
 frm.height = get_screen_height(270);
 frm.bottomMargin = get_screen_height(80);
 frm.gravity = Gravity.CENTER;


 TextView title = (TextView) findViewById(R.id.trump_title);

 frm = (FrameLayout.LayoutParams)title.getLayoutParams();//here title is in frame layout
frm.gravity = Gravity.CENTER_HORIZONTAL;
 frm.topMargin = get_screen_height(24);

##对所有视图##

使用上述方法

创建方法

private int get_screen_width(int w) {
    return deviceWidth * w / 720;// dimention(720)(width) of screen in which you set layout
}

private int get_screen_height(int h) {
    return deviceHeight * h / 1280;// dimention(1280)(height) of screen in which you set layout
}