2015-02-11 16 views
9

Tôi có chế độ xem recylcler, nhưng có vẻ như không thực hiện phương pháp setGravity.Lực hấp dẫn của RecyclerView

Danh sách của tôi được căn chỉnh theo chiều ngang, nhưng tôi cần nó được căn giữa. Làm thế nào để làm điều đó?

fragment_my_zone_item_controlled.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/dark_grey"> 

    <FrameLayout 
     android:id="@+id/fragment_my_zone_item_controlled_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/fragment_my_zone_item_controlled_tabs" 
     android:layout_marginBottom="12dp" 
     android:layout_marginTop="12dp" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/fragment_my_zone_item_controlled_tabs" 
     android:layout_width="match_parent" 
     android:layout_height="44dp" 
     android:background="@color/action_bar_color" 
     android:scrollbars="none" /> 

</RelativeLayout> 

This is how it looks

Đây là cách hiển thị, tôi cần nó để được tập trung

Cảm ơn các bạn

Trả lời

5

Tôi biết điều này là rất muộn Câu trả lời, Có thể một số người khác đã được giúp đỡ từ việc này vì vậy hãy thử thêm như thế này

<android.support.v7.widget.RecyclerView 
     android:id="@+id/fragment_my_zone_item_controlled_tabs" 
     android:layout_width="wrap_content" 
     android:layout_height="44dp" 
     android:layout_centerHorizontal="true" 
     android:background="#dddddd" 
     android:scrollbars="none" /> 

thêm android:layout_centerHorizontal="true" phương pháp

+2

Thay đổi quan trọng khác là đặt 'android: layout_width' thành' wrap_content'. –

+0

Điều này sẽ tập trung một 'RecyclerView' trong cha mẹ của nó -' RelativeLayout'. Nhưng điều này sẽ không ảnh hưởng đến các mục của Recycler hoặc khi chiều rộng của Recycler là 'match_parent'. –

+0

RecyclerView không có thuộc tính như 'layout_center_horizon' –

1

Nếu câu trả lời khác không giúp bạn, tôi phải đối mặt với một vấn đề trong RecyclerView theo đó nội dung không được bọc một cách hiệu quả, dẫn đến bố trí bị buộc phải một góc này hay cách khác.

Câu trả lời cho tôi là tạo một lớp tùy chỉnh mở rộng LinearLayoutManager. Bạn có thể thử điều này và sau đó đặt trọng lực theo chương trình.

Đây là liên kết đến số question của tôi và bài đăng nơi tôi nhận được idea for my answer.

1

(Trong trường hợp ai đó đang tìm kiếm câu trả lời như tôi), tôi đã thử cả giải pháp và không hoạt động đối với tôi, dưới đây là những gì tôi đã làm. Tôi tính toán độ rộng màn hình & rộng quan điểm của tôi, sau đó trong RecyclerView.ViewHolder tôi thêm lề sang bên trái cho tôi xem (đó là một tỷ lệ trong trường hợp của tôi)

DisplayMetrics metrics = new DisplayMetrics(); 
context.getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int screenWidth = metrics.widthPixels; 
long ratio = screenWidth * 90/100; 
int margin = (int) ((screenWidth - ratio)/2); 
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams((int) ratio, ViewGroup.LayoutParams.WRAP_CONTENT); 
lp1.gravity = Gravity.CENTER_VERTICAL; 
lp1.setMargins(margin, 0, 0, 0); 
itemLayoutView.setLayoutParams(lp1); 

Đó là giải pháp lộn xộn nhưng tôi không thể tìm thấy một

0

giải phápLazyFinder hoạt động nhưng chỉ trong trường hợp nếu bạn muốn có màu nền khác cho RV rồi thêm nó vào FrameLayout và đặt trọng tâm của nó thành tâm.

<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorWhite"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      /> 

</FrameLayout> 
Các vấn đề liên quan