安卓 - 如何为多个屏幕设置相同的布局

3
我是一名初学者。 我正在开发一个应用程序,遇到了布局方面的奇怪问题。 当我在小设备上运行我的应用程序时,它工作正常,但是当我在大屏幕设备上运行它时,它的布局属性会自动更改。 请告诉我是否有任何方法可以为所有屏幕大小创建单个编程布局? 我的xml是:
<RelativeLayout android:id="@+id/contents"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" >

            <ImageButton
                android:id="@+id/imgBtn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/panchangtab1" />

            <ImageButton
                android:id="@+id/imgBtn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_toRightOf="@+id/imgBtn1"
                android:background="@drawable/horoscopetab1" />
</RelativeLayout>

这里我成功获取了第一张图片,但是第二张图片大小不正确。

通过在Java中设置边距,找到屏幕大小并进行修复。 - arshad kr
@Ashish Patil,您不应该给应用程序中使用的组件设置任何固定值。将组件的宽度和高度设置为“match_parent”、“wrap_content”、“fill_parent”。 - Ram
6个回答

3

在布局中,宽度和高度要使用fill_parent、match_parent、wrap_content这些单位。此外,你需要使用weight属性控制布局的大小,而不是硬编码设置高度和宽度。

如果想要支持多种屏幕分辨率,请查看以下链接: http://developer.android.com/guide/practices/screens_support.html


1

这对我有效。

在布局声明中使用weightsum将会对你有帮助。

       <LinearLayout

                android:weightSum="100"   //This is horizontal layout so will work for width
                android:layout_height="wrap_content"
                android:orientation="horizontal"


                 >

                <EditText
                    android:id="@+id/txt1"
                    android:layout_weight="40" //40% for text1
                    android:layout_height="40dp"  
                    >
                </EditText>

                <EditText
                    android:id="@+id/txt2"
                    android:layout_weight="60" //60% for text2
                    android:layout_height="50dp"  

                    >
                </EditText>
            </LinearLayout>

你知道相对布局中如何调整宽度吗? - Ashish Patil
针对布局:应当使用match_parent和wrap_content。 - Rohan Kadu

0

您可以使用不同的限定符为不同的屏幕创建多个布局,例如:

layout-hdpi/mylayout

你应该阅读this


0

0

在您的布局文件中,应使用match_parent和wrap_content。可以在RelativeLayout或LinearLayout等布局中使用它们。

如果您想让ImageView、TextView、Button等元素覆盖整个屏幕宽度,则应在layout_width上使用fill_parent。同样,如果您想让ImageView、TextView、Button等元素覆盖整个屏幕高度,则应在layout_height上使用fill_parent

您可以查看LayoutParams以获取更多信息。


0

您可以使用match_parent,wrap_content,fill_parent等属性,而不需要创建硬编码布局文件。使用9 patch图像,而不是静态的。并非所有情况都适用于使用9 patch,但当您发现某些图像可以在9 patch中工作时,请将其制作为9 patch。这仅适用于ImageView而不是所有组件,剩余的组件如TextViews,EditText等,您可以自己创建,但不要创建硬编码布局。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接