2014-02-24 82 views
1

我在製作一個公交時刻表應用程序。主要活動中有很多按鈕。我的手機有一個高清屏幕,所以我製作了屏幕分辨率的按鈕。定位在不同佈局的按鈕

My main activity

我怎樣才能使按鈕的,以適應各種屏幕分辨率的位置?

+0

發佈UR XML文件 – kId

+0

嘗試與match_parent和體重。將layout_width設置爲0並設置權重1.它會有所幫助 – Amsheer

回答

1

如果您通過XML佈局創建按鈕,然後將它們用浸尺寸(密度獨立像素)而不是px(正常像素)。如果您從資源中提取這些圖像,那麼您將必須擁有項目結構(hdpi,mdpi,ldpi等)中相應文件夾內放置的所有屏幕分辨率的資源 - 但這會佔用相當多的內存。

0

您可以使用GridView用於此目的。

0

如果您不想使用GridView,可以使用線性和垂直方向作爲主佈局,並將線性佈局包含「水平」方向的按鈕作爲行。 要使按鈕適合不同分辨率的相同空間,可以使用layout_weigth屬性。

類似的東西

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

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

     <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

     <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

     <!-- yo can define as much buttons as you want --> 

     </LinearLayout> 

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

     <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

     <Button 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

     <!-- yo can define as much buttons as you want --> 

</LinearLayout>  

<!-- and so on --> 


</LinearLayout> 

定義佈局這種方式可能不適合peformance所以小心使用

0

您可以使用網格視圖與7行

GridView gridView = new GridView(context); 
    gridView.setNumColumns(7);