RelativeLayout并不适合所有屏幕尺寸的android

时间:2015-06-02 14:01:15

标签: android android-relativelayout

我正在开发我的第一个应用程序在android中,我坚持我的xml布局。在relativelayout下我的布局中有5个垂直放置的按钮。我使用索尼xperia M并根据我的屏幕尺寸布局看起来非常好,但对于大屏幕布局搞砸了。当我在具有大屏幕尺寸的设备中运行我的应用程序时,按钮之间没有适当的间隙。如何使我的布局适合所有屏幕尺寸?

string s1 = "Hello";
string s2 = "el";
if(strstr(s1.c_str(),s2.c_str()))
{
   cout << " S1 Contains S2";
}

1 个答案:

答案 0 :(得分:0)

您应该考虑将按钮包装在另一个布局中,例如LinearLayout。如果你想让你的按钮在屏幕上均匀分布,你应该使用这样的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button2" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button3" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button4" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button5" android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>

非常重要的是将children的高度设置为match_parent并设置正确的layout_weight值。