Android屏幕分辨率问题

时间:2014-05-07 08:47:54

标签: android android-layout screen screen-resolution

我有两片药片。我已经运行了一个测试分辨率应用来获取屏幕参数。 Thouse是结果。

  

1)Coby MID7042 7":低密度,比例:0.75,120dpi,480 x 764,   大屏幕布局,可绘制LDPI。

     

2)BQ Elcano 7":高密度,比例:1.5,240dpi,800 x 1208,布局   适用于大屏幕,可绘制的HDPI。

我开发了一个适合Coby屏幕的Android应用程序,但在BQ中,屏幕在右侧和底部都被剪切。也就是说,屏幕比您在屏幕上看到的要大。我认为BQ屏幕比Coby更好更大,但我不知道它为什么不适合屏幕。
在我开发的Android应用程序中,屏幕的xml文件进入res / layout文件夹。我总是使用sp和dp。
我已经阅读过关于支持多个屏幕"等等,但在这种情况下,我认为BQ应该使用相同的布局显示适合的屏幕。但事实并非如此 有人可以帮我解决这个问题吗?我不知道如何解决这个问题,因为两个平板电脑的布局很大 谢谢。
编辑。这是一个在右侧剪切的布局(最新的EditText被剪切):

    <LinearLayout
style="@style/GroupBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:baselineAligned="true" >
<LinearLayout
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
    android:id="@+id/TextView08"
    style="@style/etstyle1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/Box" />
    <EditText
    android:id="@+id/eBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:layout_weight="1"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:enabled="true"
    android:gravity="right"
    android:inputType="number|numberSigned|numberDecimal"
    android:selectAllOnFocus="true" >
    </EditText>
</LinearLayout>
<LinearLayout
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:orientation="vertical" >
    <TextView
    android:id="@+id/TextView06"
    style="@style/etstyle1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/Uds" />
    <EditText
    android:id="@+id/euds"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:enabled="true"
    android:gravity="right"
    android:inputType="number|numberSigned|numberDecimal"
    android:selectAllOnFocus="true" />
</LinearLayout>
<LinearLayout
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:orientation="vertical" >
    <TextView
    android:id="@+id/TextView09"
    style="@style/etstyle1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|left"
    android:paddingLeft="10dp"
    android:text="@string/Tar" />
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:paddingBottom="5dp" >
    <EditText
        android:id="@+id/etar"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/roundedittext"
        android:ems="10"
        android:enabled="true"
        android:gravity="center_vertical|right"
        android:inputType="number"
        android:selectAllOnFocus="true" />
    <Button
        android:id="@+id/btnTarifa"
        style="@style/btnSer"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical|center_horizontal|center"
        android:layout_marginTop="2dp"
        android:text="@string/Inter" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="135dp"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:orientation="vertical" >
    <TextView
    android:id="@+id/etit"
    style="@style/etstyle1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/price" />
    <EditText
    android:id="@+id/eprice"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:enabled="true"
    android:gravity="center_vertical|right"
    android:inputType="numberDecimal"
    android:selectAllOnFocus="true" />
</LinearLayout>
</LinearLayout>

4 个答案:

答案 0 :(得分:2)

DroidUsers的回答有点令人困惑,但方法很有效...... 寻找更有效的例子:     http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf

答案 1 :(得分:0)

我使用了一种方法,我在标准设备中设计了我想要的布局,并使用单独的类动态应用所有布局相关值(例如:填充,边距等)。并通过计算当前设备配置与我已存储的标准的比率,

我会给你我的示例代码。只需仔细阅读并找到相应的方法

public class ListViewDimensions 
{
    private Context _context;
    private Configuration config;
    private Display display;
    private int _width;
    private int _height;
    private int _portOrLand;
    private int _ORIENTATION_LAND=2;
    private int _ORIENTATION_PORT=1;
    private DisplayMetrics metrics;
    private Dimension dimension;


//***************************** PORTRAIT ********************************\\

            public int PlistLayoutPaddingLeft=16;
            public int PlistLayoutPaddingRight=16;  
            public int PlistLayoutPaddingTop=16;
            public int PlistLayoutPaddingBottom=15;

            public int PsuraNoTextsize=10;
            public int PsuraNameTextsize=20;
            public int PayaNoTextsize=10;
            public int PsuraNameWidth=180;  

            public int PpageNoTextsize=10;
            public int PpageNoPadding=2;   

            public int PmalayalamFontsize;
            public int ParabiFontsize;

            public int PsuraHeadHeight=40;

//----------------------------------------------------------------------------------------




//***************************** LANDSCAPE IN TABS ********************************\\




            public int LTlistLayoutPaddingLeft=35;
            public int LTlistLayoutPaddingTop=30;
            public int LTlistLayoutPaddingRight=35;         
            public int LTlistLayoutPaddingRightRightPage=35;            
            public int LTlistLayoutPaddingBottom=5;


            public int LTpageNoTextsize=13;
            public int LTpageNoPadding=0;   

            public int LTsuraNoTextsize=15;
            public int LTsuraNameTextsize=25;
            public int LTayaNoTextsize=15;
            public int LTsuraNameWidth=375;  


            public int LTmalayalamFontsize;
            public int LTarabiFontsize;

            public int LTsuraHeadHeight=60;

//----------------------------------------------------------------------------------------




//***************************** LANDSCAPE IN NON-TABS ********************************\\

            public int LlistLayoutPaddingLeft=21;
            public int LlistLayoutPaddingRight=20;  
            public int LlistLayoutPaddingTop=16;
            public int LlistLayoutPaddingBottom=17;

            public int LsuraNoTextsize=10;
            public int LsuraNameTextsize=20;
            public int LayaNoTextsize=10;
            public int LsuraNameWidth=330;  

            public int LpageNoTextsize=13;
            public int LpageNoPadding=3;   

            public int LmalayalamFontsize;
            public int LarabiFontsize;

            public int LsuraHeadHeight=40;

//----------------------------------------------------------------------------------------

            public float xScale;
            private float yScale;
            private double diagonalInches;


public class Dimension
{

    public int listLayoutPaddingLeft;
    public int listLayoutPaddingRight;  
    public int listLayoutPaddingTop;
    public int listLayoutPaddingBottom;

    public int pageNoTextsize;
    public int pageNoPadding;   

    public int suraNoTextsize;
    public int suraNameTextsize;
    public int ayaNoTextsize;
    public int suraNameWidth;  

    public int malayalamFontsize;
    public int arabiFontsize;
    public int suraHeadHeight;
    public String bismi;

}

public ListViewDimensions.Dimension getDimensions(Context c)
{

     PmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize;
     ParabiFontsize=MyApp.appSettings.listMalayalamFontSize;

     LTmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize*2;
     LTarabiFontsize=MyApp.appSettings.listMalayalamFontSize*2;

     LmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize;
     LarabiFontsize=MyApp.appSettings.listMalayalamFontSize;


    dimension=new Dimension();

    _context=c;
    config =c.getResources().getConfiguration();
    display=((Activity) c).getWindowManager().getDefaultDisplay(); 


    _width = display.getWidth();
    _height =display.getHeight();

    if(config.orientation==config.ORIENTATION_LANDSCAPE)
        _portOrLand=_ORIENTATION_LAND;
    else 
        _portOrLand=_ORIENTATION_PORT;

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

    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;    
    int density=metrics.densityDpi;
    float widthDpi = metrics.xdpi;
    float heightDpi = metrics.ydpi;

    if(density/widthDpi>1.2)
    {
        widthDpi=density;
        heightDpi=density;
    }

    float widthInches = widthPixels / widthDpi;
    float heightInches = heightPixels / heightDpi;  
    diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));

    if(diagonalInches>=6.7 && _portOrLand==_ORIENTATION_LAND)
    {
        //TODO :   for landscape in tabs
        xScale =  (_width/2)/(float)640 ;            
        yScale = _height/(float)752 ; 



        dimension.listLayoutPaddingLeft=LTlistLayoutPaddingLeft;
        dimension.listLayoutPaddingRight=LTlistLayoutPaddingRight;  
        dimension.listLayoutPaddingTop=LTlistLayoutPaddingTop;
        dimension.listLayoutPaddingBottom=LTlistLayoutPaddingBottom;

        dimension.pageNoTextsize=LTpageNoTextsize;
        dimension.pageNoPadding=LTpageNoPadding;  

        dimension.malayalamFontsize=LTmalayalamFontsize;
        dimension.arabiFontsize=LTarabiFontsize;

        dimension.suraNoTextsize=LTsuraNoTextsize;
        dimension.suraNameTextsize=LTsuraNameTextsize;
        dimension.ayaNoTextsize=LTayaNoTextsize;
        dimension.suraNameWidth=LTsuraNameWidth;   
        dimension.suraHeadHeight=LTsuraHeadHeight;
        dimension.bismi="&%$";
    }
    else if( _portOrLand==_ORIENTATION_PORT)
    {
        //TODO :   for portrait in non-tab devices and tabs
        xScale =  _width/(float)320 ;
        yScale = _height/(float)480 ;



        dimension.listLayoutPaddingLeft=PlistLayoutPaddingLeft;
        dimension.listLayoutPaddingRight=PlistLayoutPaddingRight;   
        dimension.listLayoutPaddingTop=PlistLayoutPaddingTop;
        dimension.listLayoutPaddingBottom=PlistLayoutPaddingBottom;

        dimension.pageNoTextsize=PpageNoTextsize;
        dimension.pageNoPadding=PpageNoPadding;   //TODO: check whether it is mandatory

        dimension.malayalamFontsize=PmalayalamFontsize;
        dimension.arabiFontsize=ParabiFontsize;

        dimension.suraHeadHeight=PsuraHeadHeight;

        dimension.suraNoTextsize=PsuraNoTextsize;
        dimension.suraNameTextsize=PsuraNameTextsize;
        dimension.ayaNoTextsize=PayaNoTextsize;
        dimension.suraNameWidth=PsuraNameWidth;  

        if(diagonalInches<4.0)
            dimension.bismi="ó";
        else
            dimension.bismi="&%$";

    }
    else if(diagonalInches<6.7 && _portOrLand==_ORIENTATION_LAND)
    {
        //TODO :   for land in non-tab devices 
        xScale =  _height/(float)320 ;
        yScale = _width/(float)480 ; 

        dimension.listLayoutPaddingLeft=LlistLayoutPaddingLeft;
        dimension.listLayoutPaddingRight=LlistLayoutPaddingRight;   
        dimension.listLayoutPaddingTop=LlistLayoutPaddingTop;
        dimension.listLayoutPaddingBottom=LlistLayoutPaddingBottom;

        dimension.suraHeadHeight=LsuraHeadHeight;           
        dimension.suraNoTextsize=LsuraNoTextsize;
        dimension.suraNameTextsize=LsuraNameTextsize;
        dimension.ayaNoTextsize=LayaNoTextsize;
        dimension.suraNameWidth=LsuraNameWidth;  

        dimension.pageNoTextsize=LpageNoTextsize;
        dimension.pageNoPadding=LpageNoPadding;   //TODO: check whether it is mandatory

        dimension.malayalamFontsize=LmalayalamFontsize;
        dimension.arabiFontsize=LarabiFontsize;
        dimension.suraHeadHeight=LsuraHeadHeight;   
        dimension.bismi="&%$";

    }


    dimension.listLayoutPaddingLeft=(int) (dimension.listLayoutPaddingLeft*xScale);
    dimension.listLayoutPaddingRight=(int) (dimension.listLayoutPaddingRight*xScale);
    dimension.listLayoutPaddingTop=(int) (dimension.listLayoutPaddingTop*yScale);
    dimension.listLayoutPaddingBottom=(int) (dimension.listLayoutPaddingBottom*yScale);

    dimension.pageNoTextsize=(int) (dimension.pageNoTextsize*xScale);
    dimension.pageNoPadding=(int) (dimension.pageNoPadding*yScale); 

    dimension.malayalamFontsize=(int) (dimension.malayalamFontsize*xScale);
    dimension.arabiFontsize=(int) (dimension.arabiFontsize*xScale);
    dimension.suraHeadHeight=(int) (dimension.suraHeadHeight*yScale);



dimension.suraNoTextsize=(int) (dimension.suraNoTextsize*xScale);
        dimension.suraNameTextsize=(int) (dimension.suraNameTextsize*xScale);
        dimension.ayaNoTextsize=(int) (dimension.ayaNoTextsize*xScale);
        dimension.suraNameWidth=(int) (dimension.suraNameWidth*xScale);  




    return this.dimension;

}

public ListViewDimensions.Dimension updateFontSize() 
{
    if(diagonalInches>=6.7 && _portOrLand==_ORIENTATION_LAND)
    {

        xScale =  (_width/2)/(float)640 ;            
        yScale = _height/(float)752 ; 

        dimension.malayalamFontsize=LTmalayalamFontsize;
        dimension.arabiFontsize=LTarabiFontsize;
    }
    else if( _portOrLand==_ORIENTATION_PORT)
    {


        xScale =  _width/(float)320 ;
        yScale = _height/(float)480 ;

        dimension.malayalamFontsize=PmalayalamFontsize;
        dimension.arabiFontsize=ParabiFontsize;
    }
    else if(diagonalInches<6.7 && _portOrLand==_ORIENTATION_LAND)
    {
        xScale =  _height/(float)320 ;
        yScale = _width/(float)480 ; 

        dimension.malayalamFontsize=LmalayalamFontsize;
        dimension.arabiFontsize=LarabiFontsize;
    }
    dimension.malayalamFontsize=(int) (dimension.malayalamFontsize*xScale);
    dimension.arabiFontsize=(int) (dimension.arabiFontsize*xScale);

    return dimension;
}


}

答案 2 :(得分:0)

您应该尝试使用android:layout_weight而不是指定LinearLayout的android:layout_width="130dp"。这样,textview和edittexts的宽度以及不能设置为fill_parent和Android的宽度都可以根据您正在使用的设备扩展到屏幕大小。

除了线性布局的例外情况,你有一个新的编辑文本和一个按钮的线性布局,你应该再使用这个edittext和按钮的权重属性而不是它们的宽度参数。

稍微修改了一下(出于显而易见的原因,我无法保留样式和文本属性)xml外观的示例:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="5dp"
  android:layout_weight="1"
  android:baselineAligned="true" >

    <LinearLayout
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_weight="1" >

        <TextView
          android:id="@+id/TextView08"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" />
        <EditText
          android:id="@+id/eBox"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="6dp"
          android:ems="10"
          android:enabled="true"
          android:gravity="right"
          android:inputType="number|numberSigned|numberDecimal"
          android:selectAllOnFocus="true" >
        </EditText>
    </LinearLayout>
    <LinearLayout
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:orientation="vertical"
      android:layout_weight="1" >

        <TextView
          android:id="@+id/TextView06"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal" />
        <EditText
          android:id="@+id/euds"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="6dp"
          android:ems="10"
          android:enabled="true"
          android:gravity="right"
          android:inputType="number|numberSigned|numberDecimal"
          android:selectAllOnFocus="true" />
    </LinearLayout>
    <LinearLayout
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:orientation="vertical"
      android:layout_weight="1" >
        <TextView
          android:id="@+id/TextView09"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal|left"
          android:paddingLeft="10dp" />
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"
          android:paddingBottom="5dp" >
            <EditText
              android:id="@+id/etar"
              android:layout_weight="0.5"
              android:layout_height="wrap_content"
              android:layout_marginTop="5dp"
              android:ems="10"
              android:enabled="true"
              android:gravity="center_vertical|right"
              android:inputType="number"
              android:selectAllOnFocus="true" />
            <Button
              android:id="@+id/btnTarifa"
              android:layout_weight="0.5"
              android:layout_height="40dp"
              android:layout_gravity="center_vertical|center_horizontal|center"
              android:layout_marginTop="2dp" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
      android:layout_weight="1"
      android:layout_height="match_parent"
      android:layout_marginLeft="5dp"
      android:orientation="vertical" >
        <TextView
          android:id="@+id/etit"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal" />
        <EditText
          android:id="@+id/eprice"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="6dp"
          android:ems="10"
          android:enabled="true"
          android:gravity="center_vertical|right"
          android:inputType="numberDecimal"
          android:selectAllOnFocus="true" />
     </LinearLayout>
</LinearLayout>

答案 3 :(得分:0)

经过大量的研究后,我还没有找到它为什么会发生,而是一个非常快速的解决方案。我可以扩展所有将此代码添加到主要活动的应用程序:

public void changeDensity(float desiredDensity) {
//desiredDensity : ldpi = 0.75 (120dpi) , mdpi = 1 (160dpi), hdpi = 1.5 (240dpi), xhdpi = 2.0 (320dpi)
DisplayMetrics metrics = getResources().getDisplayMetrics();

metrics.density = desiredDensity;
metrics.xdpi = desiredDensity * 160;
metrics.ydpi = desiredDensity * 160;
metrics.densityDpi = (int) (desiredDensity * 160);

getResources().updateConfiguration(null, null);

}

在创建主要活动时调用此函数,使用1.25f作为desiredDensity,应用程序将缩放以适合正确的维度。我知道这不是方法。我应该改变所有的布局,但我不能用这个设备做,因为两者都使用相同的布局(大)然后,也许,我应该使用w作为res / layout中的参数。简而言之,缩放可以解决问题 由此:https://stackoverflow.com/a/12077236/1616700