0

我在RecyclerView中設置佈局時遇到問題。RecyclerView的列表項目中的佈局對齊不正確

這裏是一個佈局XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <TextView 
     android:id="@+id/dummy_text_view" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="0.5" 
     android:background="#ffffff" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" 
     android:text="Button" 
     android:textAllCaps="false" /> 

</LinearLayout> 

這裏是我的MainActivity

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

     LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 

     recyclerView.setLayoutManager(layoutManager); 

     List<String> list = new ArrayList<>(); 
     for (int i = 0; i <= 25; i++) { 
      list.add("item " + i); 
     } 

     DummyRVAdapter adapter = new DummyRVAdapter(MainActivity.this, list); 
     recyclerView.setAdapter(adapter); 

    } 

代碼適配器:

public class DummyRVAdapter extends RecyclerView.Adapter<DummyRVAdapter.Holder> { 

    private Context mContext; 
    private List<String> mList; 

    public DummyRVAdapter(Context context, List<String> list) { 
     this.mContext = context; 
     this.mList = list; 
    } 

    @Override 
    public DummyRVAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = View.inflate(mContext, R.layout.item_dummy_layout, null); 
     //View view =LayoutInflater.from(mContext).inflate(R.layout.item_dummy_layout, null); 
     return new Holder(view); 
    } 

    @Override 
    public void onBindViewHolder(DummyRVAdapter.Holder holder, int position) { 
     holder.textView.setText(mList.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     return mList.size(); 
    } 

    public class Holder extends RecyclerView.ViewHolder { 
     TextView textView; 

     public Holder(View itemView) { 
      super(itemView); 

      textView = (TextView) itemView.findViewById(R.id.dummy_text_view); 
     } 
    } 
} 

我輸出的屏幕截圖。

enter image description here

在我的佈局XML文件,我宣佈了規模爲TextView的50%和按鈕,30% 了,不過,我recyclerView沒有以正確的方式是不正確的方式顯示出。

此佈局在Listview中正常工作。

我不知道我的代碼中的問題在哪裏。我以正確的方式將佈局管理器和適配器設置爲我的RecyclerView。

編輯: 請參閱截圖它表明的TextView不分配屏幕的50%,這就是問題所在。

它我創建複雜的佈局爲Recyclerview其對齊方式不是提到的格式。

但在列表視圖

的作品

誰能幫助我在這?

在此先感謝。

+0

取出重量之和,然後按鈕及TextView的 –

+0

指定重量只是刪除'機器人:weightSum = 「1」' – GVillani82

+0

@ GVillani82我試過了但它仍然是同樣的問題,並且textview的大小甚至比這更小。請參閱屏幕截圖Textview未分配設備的50%屏幕尺寸。 – Madhan

回答

0

而不是

View view = View.inflate(mContext, R.layout.item_dummy_layout, null); 

使用:

View view = LayoutInflater.from(mContext).(R.layout.item_dummy_layout, parent, false); 
+0

將嘗試更新您 – Madhan

+0

非常感謝,男士。有用。謝謝,太多了。 :) – Madhan

+0

@ GVillani82謝謝你,它也適用於我,但你可以解釋什麼區別? – Rahul

0

作爲android:weightSum="1"但所有android:layout_weight的總和爲不等於1,因此這是無法正確顯示

你有聲明爲textView尺寸爲50%及Button爲30%,但休息剩餘由於這20%的它沒有正確顯示的原因。所以可以這樣做,要麼將textview增加到70%,要麼Button增加到30%,所以android:weightSumandroid:layout_weight總和將相等。

佈局更改爲

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="1" > 

    <TextView 
     android:id="@+id/dummy_text_view" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7" 
     android:background="#ffffff" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" 
     android:text="Button" 
     android:textAllCaps="false" /> 
</LinearLayout > 
+0

爲什麼要添加其他視圖?將「weightSum」更改爲0.8或將其刪除並不簡單? – GVillani82

+0

是的,它也可以做到。讓我改變它。 –

+0

請參閱截圖。它顯示TextView未在設備中分配50%的面積。 – Madhan

0

它沒有那是有問題的視圖TextView的可佔據其大小隻重量,但它比按鈕的大小花冤枉錢一個重要的重量即使經過或不
的文本它使用最小尺寸和最大尺寸按鈕按照它需要像在佈局做到這一點,以適應:

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

<TextView 
    android:id="@+id/dummy_text_view" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="0.5" 
    android:background="#ffffff" /> 

<Button 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" 
    android:text="Button" 
    android:max_height="30dp" 
    android:textAllCaps="false" /> 

+0

請參閱我的編輯 – Madhan