如何在android中为水平和垂直的活动设置两个不同大小的背景图像

时间:2014-01-23 09:25:23

标签: android android-layout

我想为水平和垂直的活动设置两种不同大小的背景图像。这样做,因为水平和垂直相同的图像设置都会拉伸。

例如:

Example of horizontal and vertical layouts

4 个答案:

答案 0 :(得分:5)

请浏览以下开发者链接,该链接可让您了解图片的文件夹结构,以支持横向和纵向布局。

http://developer.android.com/training/basics/supporting-devices/screens.html

区分风景和肖像图像的基本方法是将肖像图像放入可绘制文件夹,将风景图像放入 drawable-land 文件夹..但不同尺寸需要一些您将从上述开发人员链接中了解更多规范..

希望它会帮助你...... !!!

答案 1 :(得分:3)

    main.xml   

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context=".MainActivity" >

            <LinearLayout
                android:id="@+id/ln_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2"
                android:gravity="center_horizontal"
                android:layout_marginTop="10dp" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/ic_launcher" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

            </LinearLayout>
        <LinearLayout
            android:id="@+id/ln_vertical"
                android:layout_below="@+id/ln_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="2"
                android:visibility="gone"
                android:gravity="center_horizontal"
                android:layout_marginTop="10dp" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@drawable/ic_launcher" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_weight="1"
                    android:layout_marginTop="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

            </LinearLayout>

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ln_vertical"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="53dp"
            android:text="Button" />

        </RelativeLayout>




    main.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LinearLayout ln_hori;
        final LinearLayout ln_ver;
        Button b;
        b=(Button)findViewById(R.id.button1);
        ln_hori=(LinearLayout)findViewById(R.id.ln_horizontal);
        ln_ver=(LinearLayout)findViewById(R.id.ln_vertical);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(ln_hori.getVisibility()==View.VISIBLE)
                {
                    ln_hori.setVisibility(View.GONE);
                    ln_ver.setVisibility(View.VISIBLE);
                }else
                {
                    ln_hori.setVisibility(View.VISIBLE);
                    ln_ver.setVisibility(View.GONE);
                }

            }
        });
    }

答案 2 :(得分:2)

在res文件夹中创建 layout-land 目录,并将布局XML文件的横向版本放在该目录中。

例如,您使用main.xml布局,然后必须将main.xml文件放入两个方向。

喜欢这个

对于potrait:

  

res / layout /

       main.xml

对于横向模式:

 res/layout-land/

       main.xml

答案 3 :(得分:1)

在Drawable中已经存在像 drawable-hdpi 这样的文件夹,它默认支持纵向模式,你必须为横向模式创建文件夹,如 drawable-hdpi-land 并在其中添加图像。 纵向模式的图片尺寸:

320dp:典型的手机屏幕(240x320 ldpi,320x480 mdpi,480x800 hdpi等)。 480dp:像Streak(480x800 mdpi)这样的tweener平板电脑。 600dp:7“平板电脑(600x1024 mdpi)。 720dp:10英寸平板电脑(720x1280 mdpi,800x1280 mdpi等)。

横向模式的图片尺寸:

与portarit模式中提到的图像大小相反 例如:纵向模式下的图像尺寸为240x320 ldpi,横向模式下为320x240