边距dp单位在不同的屏幕中无法正确缩放布局

时间:2012-07-13 08:53:10

标签: android scale

我有简单的tableLayout,它显示了独立单元dp的问题。

我认为在我的 layout_margin 属性中使用dp单元 - 布局将在任何设备上调整大小而没有问题。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_margin="40dp"
    >

<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> 
<TextView 
android:text="@string/start_login_username"
android:id="@+id/start_login_username" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
/>


<EditText 
android:id="@+id/start_login_EditTxt1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
/>

</TableRow>


<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView 
android:text="@string/start_login_password"
android:id="@+id/start_login_password" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>


<EditText 
android:id="@+id/start_login_EditTxt2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:inputType="textPassword"
/>

</TableRow>


<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="@string/start_login_cancel"
android:id="@+id/start_login_cancel" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
>

</Button>


<Button
android:text="@string/start_login_login"
android:id="@+id/start_login_ButtonLogin" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
>

</Button>


</TableRow>



</TableLayout>

但是,测试这种布局,我们可以看到dp在布局边缘上并不那么独立。

这里是三星Nexus的截图(大规模):

Samsung Galaxy

这里是LG Optimus GT 540的屏幕截图(错误比例):

enter image description here

我正在寻找StackOverflow中的类似线程,以及可能的解决方案,在任何屏幕上都有正确的比例:

  • 根本不使用margin和padding,但总是使用tableLayout并添加 moc具有一些权重的视图,在视图之间添加空间(因为的不好) 污染xml布局。无论如何,它看起来很有希望。)

  • 自己计算独立单元,并以编程方式重新缩放(对我不好,因为我的所有单位都在xml中)

  • 对不同的屏幕使用不同的布局(不好,因为我懒得为每个视图重新创建布局并支持它们)

问题:

如何使用相同的布局解决所有屏幕的布局缩放问题?

如何在屏幕上使保证金正常工作?

您对我的两个解决方案的看法。这是好习惯吗? (特别是第一个)?

谢谢

1 个答案:

答案 0 :(得分:0)

只需使用带有权重属性的linearlayout,如果是想要自动调整的宽度,请记得设置width =“0dp”。

你的xml应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_gravity="center" 
    android:orientation="horizontal"  >

    <TextView
        android:text="Your name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_weight="1"
        android:layout_width="0dp"
        android:minWidth="80dp"
        android:layout_height="wrap_content" />
</LinearLayout>
相关问题