Android views not scaling on different sized devices

纵然是瞬间 提交于 2019-12-11 23:24:00

问题


I am using eclipse I have views with images and buttons with their width and height set in dp:

<Button
    android:id="@+id/btn_menu_games"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center"
    android:background="@drawable/ico_games"
    android:onClick="goToGames" />

But when I use a different device the images and buttons do not scale relative to the screen, so they are really big on a small screen and really small on a big screen. Am I doing something wrong in the view? Do I need to set anything in the Manifest?

Thanks.

Full code:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_portrait"
    android:gravity="center_horizontal"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="234dp"
        android:layout_height="156dp"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/flag" />


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginRight="20dp" >




                <Button
                    android:id="@+id/btn_menu_games"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:background="@drawable/ico_games"
                    android:onClick="goToGames" />




        <com.academyGeneric.MyTextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="#8c0101"
            android:text="Games"
            android:layout_marginBottom="5dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />


        <Button
            android:id="@+id/btn_menu_lessons"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:background="@drawable/ico_lessons" />


       <com.academyGeneric.MyTextView
           android:id="@+id/textView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:text="Lessons"
           android:layout_marginBottom="5dp"
           android:textColor="#8c0101"
           android:textAppearance="?android:attr/textAppearanceLarge" />


          <Button
              android:id="@+id/btn_menu_settings"
              android:layout_width="70dp"
              android:layout_height="70dp"
              android:layout_gravity="center"
              android:background="@drawable/ico_settings"
              android:onClick="goToSettings" />

        <com.academyGeneric.MyTextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Settings"
            android:textColor="#8c0101"
            android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


<LinearLayout
     android:id="@+id/linearLayout2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:layout_marginLeft="20dp" >



            <Button
                android:id="@+id/btn_menu_dictionary"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:background="@drawable/ico_dictionary"
                android:onClick="goToDictionary" />


        <com.academyGeneric.MyTextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Dictionary"
            android:layout_marginBottom="5dp"
            android:textColor="#8c0101"
            android:textAppearance="?android:attr/textAppearanceLarge" />


        <Button
            android:id="@+id/btn_menu_statistics"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:background="@drawable/ico_stats" />

        <com.academyGeneric.MyTextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Statistics"
        android:layout_marginBottom="5dp"
        android:textColor="#8c0101"
        android:textAppearance="?android:attr/textAppearanceLarge"
         />


        <Button
            android:id="@+id/close"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:background="@drawable/ico_exit"
            android:onClick="exit" />

        <com.academyGeneric.MyTextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Exit"
        android:textColor="#8c0101"
        android:textAppearance="?android:attr/textAppearanceLarge"
         />

        </LinearLayout>            

    </LinearLayout>

</LinearLayout>

回答1:


If you really need it to have a certain aspect ratio, you should definitely consider using weights.

If there are two buttons in a linear layout parent, with both weight 1, they will both take up
1/2 the width of the screen

proportion of button relative to screen = (view_weight/(total_weights of all views)

Take a look here: http://blog.stylingandroid.com/archives/297

Otherwise, DP is correct.



来源:https://stackoverflow.com/questions/10141054/android-views-not-scaling-on-different-sized-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!