0

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>
4

1 回答 1

1

如果你真的需要它有一定的纵横比,你绝对应该考虑使用权重。

如果一个线性布局父级中有两个按钮,两个按钮的权重都为 1,它们都将占据
屏幕宽度的 1/2

按钮相对于屏幕的比例 = (view_weight/(total_weights of all view)

看看这里:http: //blog.stylingandroid.com/archives/297

否则,DP 是正确的。

于 2012-04-13T18:50:12.307 回答