简体   繁体   English

多屏支持

[英]Multiple Screen Support

I have some problem with multiple screen support, I work with dp(dpi) for specify the layout_heigth and layout_width and I hope that is the better way to support multiple screen, but when I tried with two smartphone I meet two different result. 我在多屏支持方面遇到一些问题,我使用dp(dpi)来指定layout_heigth和layout_width,希望这是支持多屏的更好方法,但是当我尝试使用两部智能手机时,遇到了两种不同的结果。

I give an example, this is a layout I use: 我举一个例子,这是我使用的布局:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/cities_main_layout"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

       <ListView
          android:id="@+id/citieslist"
          android:layout_width="wrap_content" 
          android:layout_height="320dip"
          android:layout_gravity="center_vertical"
          android:layout_below="@id/cities_main_layout"
       />

       <LinearLayout 
          android:id="@+id/cities_button_layout"
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/citieslist"
          android:layout_gravity="center_vertical">   
        <Button 
           android:id="@+id/bycountry"
           android:layout_height="50dip"
           android:layout_width="105dip"
           android:background="@drawable/buttonmarket"
           android:text="@string/button_bycountry"
          />
        <Button 
           android:id="@+id/top10"
           android:layout_height="50dip"
           android:layout_width="105dip"
           android:background="@drawable/buttonmarket"
           android:text="@string/button_top10"
          />
        <Button 
           android:id="@+id/recommended"
           android:layout_height="50dip"
           android:layout_width="105dip"
           android:background="@drawable/buttonmarket"
           android:text="@string/button_recommended"
          />

       </LinearLayout>

    </RelativeLayout>

The button are at the bottom of the layout, and I see two different result: 该按钮位于布局的底部,我看到两个不同的结果:

http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png http://img600.imageshack.us/img600/5513/htcmagicg2.png

http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png http://img441.imageshack.us/img441/6440/samsunggalaxys.png

In the last smartphone I can see the buttons, instead in the first I cannot...what's wrong? 在最后一部智能手机中,我可以看到按钮,而在第一部智能手机中,我无法...出了什么问题?

I have to write a layout for any set of screen??!!! 我必须为任何一组屏幕编写一个布局?

Your ListView has 您的ListView有

android:layout_height="320dip"

Now if the phone screen is smaller, it will not fit. 现在,如果手机屏幕较小,则将无法容纳手机。

Try doing this instead: (Edited due to comments. This is displayed correcty in eclipse) 尝试改为执行此操作:(由于注释而被编辑。在Eclipse中正确显示)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FF0000"
    android:layout_above="@+id/linlay">
</ListView>
<LinearLayout 
    android:id="@+id/linlay"    
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="30dip"
    android:background="#00FF00"
    android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>

Thant should fix it I think. 我认为Thant应该修复它。

Cheers 干杯

As others have indicated, your problem is that you hardwired in a size for the ListView . 正如其他人指出的那样,您的问题是您硬连接了ListView的大小。 If you want a business rule of "have the buttons at the bottom and have the ListView fill up the rest", you actually need to write code that implements "have the buttons at the bottom and have the ListView fill up the rest". 如果要使业务规则“在底部具有按钮,并让ListView填充其余部分”,则实际上需要编写实现“在底部具有按钮,并让ListView填充其余部分”的代码。

There are two main approaches here: 这里有两种主要方法:

  1. Use a LinearLayout parent for the buttons and the ListView . 对按钮和ListView使用LinearLayout父级。 Use android:layout_height="0px" and android:layout_weight="1" for the ListView . android:layout_height="0px"android:layout_weight="1"用于ListView Use a regular android:layout_height for the buttons (presumably in their own LinearLayout ) and no android:layout_weight for for them 对按钮使用常规的android:layout_height (大概在它们自己的LinearLayout ),对它们不使用android:layout_weight

  2. Use a RelativeLayout parent for the buttons and the ListView . 对按钮和ListView使用RelativeLayout父级。 Define the buttons as having android:layout_alignParentBottom="true" . 将按钮定义为具有android:layout_alignParentBottom="true" Define the ListView as having a ndroid:layout_alignParentTop="true" and android:layout_above="..." , where the ... is the ID of the buttons' LinearLayout . ListView定义为具有ndroid:layout_alignParentTop="true"android:layout_above="..." ,其中...是按钮的LinearLayout的ID。

I would say it's because you are specifically declaring a height for your ListView and then laying the LinearLayout that holds your buttons at the bottom. 我想说这是因为您要特别声明ListView的高度,然后放置将按钮保持在底部的LinearLayout Try changing it instead of being at the bottom of the ListView to something like 尝试将其更改,而不是放在ListView的底部,例如

<LinearLayout 
          android:id="@+id/cities_button_layout"
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:alignparentbottom="true"
          android:layout_gravity="center_vertical">   

I'm not entirely sure if align_parent_bottom is the 100% correct spelling of that. 我不确定align_parent_bottom是否为100%正确的拼写。

Well, others have beaten me to it while I was typing, haha, but yeah, you're hardwiring a lot of things that shouldn't be, both the ListView and the Buttons. 好吧,当我打字时,其他人都在击败我,哈哈,但是,是的,您很难使用ListView和Buttons这许多本不应该的东西。 Take a look at this: 看看这个:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cities_main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >      
    <LinearLayout 
        android:id="@+id/cities_button_layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >   
        <Button 
            android:id="@+id/bycountry"
            android:layout_height="50dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonmarket"
            android:text="@string/button_bycountry"
            />
        <Button 
            android:id="@+id/top10"
            android:layout_height="50dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonmarket"
            android:text="@string/button_top10"
            />
        <Button 
            android:id="@+id/recommended"
            android:layout_height="50dip"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonmarket"
            android:text="@string/button_recommended"
            />
    </LinearLayout>

    <ListView
        android:id="@+id/citieslist"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_above="@id/cities_button_layout"
        />           
</RelativeLayout>
  1. You have android:orientation on your RelativeLayout, which isn't actually an attribute that RelativeLayout contains. 您的RelativeLayout上有android:orientation ,它实际上不是RelativeLayout包含的属性。

  2. You should use the layout_weight attribute rather than hardwiring sizes for the Buttons. 您应该为按钮使用layout_weight属性,而不是硬接线尺寸。 In my example, all buttons have a width of fill_parent , and a weight of 1. This makes them distribute the space evenly. 在我的示例中,所有按钮的宽度为fill_parent ,权重为1。这使它们均匀地分配了空间。

  3. List the fixed button layout first, setting it to alignParentBottom="true" . 首先列出固定按钮的布局,将其设置为alignParentBottom="true" Then set the ListView to fill_parent, and layout_above your button layout. 然后设置ListView控件来FILL_PARENT,并layout_above您的按键布局。 This keeps the button layout at the bottom, and makes the ListView take all the space above your buttons. 这样可以使按钮布局保持在底部,并使ListView占据按钮上方的所有空间。

  4. Tada! 多田!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM