2012-07-10 103 views
8

我正在製作一個應用程序,我希望從右到左填充gridview「使用它從右到左語言」,我試圖讓gridView重力設置爲正確但它沒有工作...我的gridview佈局如下。從右邊開始GridView元素

在此先感謝

<RelativeLayout 
    android:id="@+id/grid_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/show_info" 
    android:gravity="right" > 

    <GridView 
     android:id="@+id/channel_grid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:columnWidth="90dp" 
     android:gravity="right" 
     android:horizontalSpacing="10dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" > 
    </GridView> 
</RelativeLayout> 

回答

4

我不認爲GridView控件的設計銘記的語言。它更像是安排縮略圖的可視化組件。

您可以通過修改適配器並讓其顛倒每行每個項目的順序來解決該問題。但是,您需要告訴適配器每行的項目數量才能完成這項工作。

最重要的是,如果最後一行的項目數量少於可能的數量,那麼您需要引入空的虛擬物品來填充左側。

+1

實際上,它從4.2開始考慮'layoutDirection'。我用一些額外的鏈接更新了我的答案。 – Jelle 2013-08-29 08:01:36

+0

這是一個很好的提示,謝謝。在我的應用程序中,gridView具有固定數量的列,這樣我就可以模擬rtl方向。 – 2014-05-27 10:02:47

2

您可以嘗試

android:layoutDirection="rtl" 

它通常是文字,我不知道什麼是對一個GridView的影響。 (ref

另一件事是嘗試

android:stackFromBottom="true" 

但是如果你需要訂購的物品自上而下和從右到左(ref

+0

stackFromBottom是我首先想到的,但它沒有幫助。 – user1940676 2013-08-22 15:36:25

+0

錯誤:找不到包'android'中屬性'layoutDirection'的資源標識符 – 2013-09-23 13:42:39

+0

刪除該答案 – 2013-09-23 13:43:02

5

我認爲它不會工作您最好的猜測是複製GridView source中的代碼,並將測量/佈局更改爲從右向左使用。

更新:視圖的佈局方向僅在API級別17(4.2)中引入。 如果您在4.24.1比較GridView.java,你可以看到,例如在方法makeRow()中,是的layoutDirection自4.2考慮,所以應該努力!

您是否嘗試在API Level 17上使用android:layoutDirection="rtl"執行代碼?

+0

是的我已經嘗試了API 17上的rtl,它沒有做到這一點。 – user1940676 2013-08-29 18:47:18

3

你可以試試這個。有你的GridView在佈局文件有下列屬性

android:layoutAnimation="@anim/layout_grid_right_to_left" 

創建anim文件夾,其中您cotents

<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
    android:columnDelay="0.5" 
    android:directionPriority="row" 
    android:direction="right_to_left" 
    android:animation="@anim/fade" /> 

添加layout_grid_right_to_left.xml此外,在同一anim文件夾內容添加fade.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:fromAlpha="0.0" android:toAlpha="1.0" 
    android:duration="@android:integer/config_longAnimTime" /> 

這些僅供參考。請隨意添加/刪除適合您的需求/要求的屬性。

2

可以在XML嘗試一下本作你的GridView及其所有項目:

android:rotationY="180" 

您還可以使用android:layoutDirection="rtl"。它可以解決你的問題。

+1

完美ans.thanks :) – Jas 2015-11-02 08:28:17

1

有一個愚蠢的做法,但它的工作完美。

只是在gridview和項目容器(網格視圖單元格的自定義視圖)中製作android:ScaleX=-1,因此您的佈局將從右向左開始!

希望這個幫助

+0

大聲笑。它的工作原理是android:ScaleX =「 - 1」。 – 2016-07-23 11:10:05