不支持多尺寸屏幕的布局文件

时间:2013-11-25 11:36:29

标签: android xml layout

我开发了一个xml文件。该文件的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#257D87" >

    <TextView
        android:id="@+id/before_campaign_tV1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:text="@string/before_campaign_text1"
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt1_dim" />

    <TextView
        android:id="@+id/before_campaign_tV2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/before_campaign_tV1"
        android:gravity="center"
        android:text="@string/before_campaign_text2"
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt2_dim" />

    <TextView
        android:id="@+id/before_campaign_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_tV2"
        android:text="@string/before_campaign_time_text"
        android:textColor="#FFFFFF"
        android:textSize="@dimen/before_campaign_txt1_dim" />

    <TextView
        android:id="@+id/before_campaign_tV3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_timer"
        android:text="@string/before_campaign_desc_text"
        android:textColor="#ED1847"
        android:textSize="@dimen/before_campaign_txt2_dim" />

    <TextView
        android:id="@+id/before_campaign_tV4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_tV3"
        android:text="to go "
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt3_dim" />

    <Button
        android:id="@+id/before_campaign_btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#BF9A54"
        android:textColor="#FFFFFF"
        android:layout_below="@+id/before_campaign_tV4"
        android:paddingLeft="@dimen/before_campaign_btn1_dim"
        android:paddingRight="@dimen/before_campaign_btn1_dim"
        android:text="@string/before_campaign_button1" />

    <Button
        android:id="@+id/before_campaign_btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#BF9A54"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_btn1"
        android:paddingLeft="@dimen/before_campaign_btn2_dim"
        android:paddingRight="@dimen/before_campaign_btn2_dim"
        android:text="@string/before_campaign_button2" />

</RelativeLayout>

但是此代码的输出在不同大小的屏幕上显示不同的输出屏幕。我不明白为什么?我正在努力将相对布局转移到所有布局。但没有结果。

enter image description here

请提供宝贵意见,帮助我。我怎么解决这个问题 ?我只想要以下屏幕。 enter image description here

3 个答案:

答案 0 :(得分:0)

像这样更改XML文件,

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#257D87" >

<ScrollView
 android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <TextView
        android:id="@+id/before_campaign_tV1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:text="@string/before_campaign_text1"
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt1_dim" />

    <TextView
        android:id="@+id/before_campaign_tV2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/before_campaign_tV1"
        android:gravity="center"
        android:text="@string/before_campaign_text2"
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt2_dim" />

    <TextView
        android:id="@+id/before_campaign_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_tV2"
        android:text="@string/before_campaign_time_text"
        android:textColor="#FFFFFF"
        android:textSize="@dimen/before_campaign_txt1_dim" />

    <TextView
        android:id="@+id/before_campaign_tV3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_timer"
        android:text="@string/before_campaign_desc_text"
        android:textColor="#ED1847"
        android:textSize="@dimen/before_campaign_txt2_dim" />

    <TextView
        android:id="@+id/before_campaign_tV4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_tV3"
        android:text="to go "
        android:textColor="#E6E0D2"
        android:textSize="@dimen/before_campaign_txt3_dim" />

    <Button
        android:id="@+id/before_campaign_btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#BF9A54"
        android:textColor="#FFFFFF"
        android:layout_below="@+id/before_campaign_tV4"
        android:paddingLeft="@dimen/before_campaign_btn1_dim"
        android:paddingRight="@dimen/before_campaign_btn1_dim"
        android:text="@string/before_campaign_button1" />

    <Button
        android:id="@+id/before_campaign_btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#BF9A54"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:layout_below="@+id/before_campaign_btn1"
        android:paddingLeft="@dimen/before_campaign_btn2_dim"
        android:paddingRight="@dimen/before_campaign_btn2_dim"
        android:text="@string/before_campaign_button2" />
</ScrollView>

</RelativeLayout>

答案 1 :(得分:0)

嗨请使用Linearlayout而不是相对布局尝试此代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/before_campaign_tV1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="Thnkyou Foe Choosing Your Favorite Gifts!"
                android:textColor="#E6E0D2"
                android:textSize="@dimen/before_campaign_txt1_dim" />

            <TextView
                android:id="@+id/before_campaign_tV2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/before_campaign_tV1"
                android:layout_weight="1"
                android:gravity="bottom"
                android:maxLines="3"
                android:singleLine="false"
                android:text="You Will Be Reminded To Enter The giveaway When these items are avilable. "
                android:textColor="#E6E0D2"
                android:textSize="@dimen/before_campaign_txt2_dim" />

            <TextView
                android:id="@+id/before_campaign_timer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/before_campaign_tV2"
                android:layout_weight="1"
                android:gravity="top"
                android:maxLines="3"
                android:singleLine="false"
                android:text="Check The CompAndSave Giveaway Page For the latest news and updates."
                android:textColor="#FFFFFF"
                android:textSize="@dimen/before_campaign_txt1_dim" />

            <TextView
                android:id="@+id/before_campaign_tV3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/before_campaign_timer"
                android:layout_weight="1"
                android:background="#ED1847"
                android:gravity="center"
                android:text="Go To GiveAway Page"
                android:textColor="#FFFFFF"
                android:textSize="@dimen/before_campaign_txt2_dim" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/before_campaign_btn1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/before_campaign_tV4"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:background="#BF9A54"
                    android:paddingLeft="@dimen/before_campaign_btn1_dim"
                    android:paddingRight="@dimen/before_campaign_btn1_dim"
                    android:text="@string/before_campaign_button1"
                    android:textColor="#FFFFFF" />

                <Button
                    android:id="@+id/before_campaign_btn2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/before_campaign_btn1"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:background="#BF9A54"
                    android:gravity="center"
                    android:paddingLeft="@dimen/before_campaign_btn2_dim"
                    android:paddingRight="@dimen/before_campaign_btn2_dim"
                    android:text="@string/before_campaign_button2"
                    android:textColor="#FFFFFF" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

答案 2 :(得分:-1)

以编程方式编写,在下面的代码中学习如何创建它:

          if (displayMetrics.density == 1.0) {
            tv.setTextSize(3 * getResources().getDisplayMetrics().density);

        }else if (displayMetrics.density == 1.5) {
            tv.setTextSize(6 * getResources().getDisplayMetrics().density);

        }else if (displayMetrics.density == 2.0) {
            tv.setTextSize(9* getResources().getDisplayMetrics().density);

        }else if (displayMetrics.density == 3.0) {
            tv.setTextSize(12 * getResources().getDisplayMetrics().density);

        }

以上代码检测设备的屏幕分辨率并制作文字大小。