android grid view place items from right to left

2019-03-15 03:00发布

I'm working on an android application with arabic version.

In one of the interfaces, I have gridView. So to display items in the correct order, I have to display items in the GridView from the right to the left (and of corse from the top to the bottom). To do that, I tried to add these attributes in the GridView :

android:gravity="right"
android:layout_gravity="right"

Unfortunately, items still displayed from the left to the right.

any idea to do it in the right way ?

6条回答
smile是对你的礼貌
2楼-- · 2019-03-15 03:03

In addition to the above answer:

android:layoutDirection="rtl"

Ideally, if you want to make it rtl aware, so only rtl in locales where it should be:

android:layoutDirection="locale"

You can also make it follow the containing layout

android:layoutDirection="inherit"
查看更多
狗以群分
3楼-- · 2019-03-15 03:05
Create one new layout-ldrtl folder and make xml file grid view .

example...
  <GridView
        android:id="@+id/popoulat_category_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:numColumns="2"
        android:scrollbars="none"
        android:horizontalSpacing="-5dp"
        android:verticalSpacing="-5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

device local language is English : enter image description here

device local language is parsian : enter image description here

查看更多
【Aperson】
4楼-- · 2019-03-15 03:08

add this line to your GridView in XML:

android:layoutDirection="rtl"
查看更多
叛逆
5楼-- · 2019-03-15 03:19

Based on traninho's answer:

  1. add this line to your GridView in XML:

    android:rotationY="180"

  2. also add the same line to your GridView item in XML:

    android:rotationY="180"

do the following:

  • On your GridActivity, delete this:

        gridView.setOnClickListener(...);
    
  • Now, go to your gridAdapter and add onClickListener to the gridImage so, your code should look like the following:

        gridImage.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    //Do something
    
                }
            });
    

This will handle clicks normally on your GridView images.

Wish that helps :)

查看更多
Emotional °昔
6楼-- · 2019-03-15 03:22

As Nibha Jain said, its right. Also you need to add a property "android:layoutAnimation" to the GridView xml as below.

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/grid"    
    android:layoutAnimation="@anim/layout_grid_right_to_left"

and also you need to define an animation file with "android:direction" attribute, which actually renders your items from right to left or any supported direction.

The layout_grid_right_to_left.xml file inside anim folder

<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" />

the @anim/fade is as below

<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" />

Its just my preferences. Please add/remove attributes that suites your needs. Play with it.

查看更多
可以哭但决不认输i
7楼-- · 2019-03-15 03:24

You can achieve this by this ugly workaround:

  1. add this line to your GridView in XML:

    android:rotationY="180"

  2. also add the same line to your GridView item in XML:

    android:rotationY="180"

In total it gives you rotation by 360° so it looks like GridView places items from right to left.

查看更多
登录 后发表回答