我开发了一个应用程序。我开发的尺寸是3.7in WVGA Nexus One。
因此,当显示大于此屏幕时,它没有问题。当我显示小于 3.7in 时,出现了问题。
这是我的 xml,我使用固定的 px 作为一些小部件。
<LinearLayout
android:id="@+id/layout_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/layout_title"
android:background="@drawable/backrepeat"
android:gravity="center_vertical|center_horizontal" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="10px"
android:paddingTop="10px" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="@+id/fixtext_name"
android:layout_width="80px"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingBottom="10px"
android:text="姓名 :"
android:textColor="#000000"
android:textSize="30px" />
<EditText
android:id="@+id/text_name"
android:layout_width="400px"
android:layout_height="wrap_content"
android:ems="15"
android:gravity="left"
android:inputType="textPersonName"
android:paddingBottom="10px"
android:textSize="30px" >
</EditText>
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我的应用程序布局的一个例子。总宽度为 480px 和高度使用
fill_parent
所以,身高没有问题。
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true"
android:requiresSmallestWidthDp="480"
android:compatibleWidthLimitDp="480"
android:largestWidthLimitDp="480" >
</supports-screens>
这是清单 xml 中的 support-screen 属性。我已经把所有东西都搞定了
问题是当我在2.7in QVGA(类似于 SG Mini)中显示时,它仅显示 240px 宽度而不是全屏项目。
我想问一下如何根据应用程序屏幕大小挤压或 re-size 应用程序?
如果可以的话,我是否需要为所有活动编写相同的代码?
4 回答
您可以指定一个只能在小屏幕上工作的新 XML 文件。
可以修改此新布局以适应较小的屏幕,而无需更改代码中的任何内容。只需确保您具有相同的 ID 等,以避免出现 NULLPOINTER 异常。
希望这可以帮助。
看起来你正在尝试使用“px”。这可能是问题所在。而是尝试“dip”获取 XML 中的 width 和 padding 属性。
这是一个非常好的例子,它解释了 px,dp,dip 等之间的区别。
Android 上的“px”,“dp”,“dip”和“sp”有什么区别?
根据小型仿真器视图设计新的.xml 文件。设计.xml 文件后,然后使用此代码。
.java 文件试试这个:
然后应用 switch case,你将传递.xml 文件 ID。
final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();