Ami*_* De 1 android android-layout android-gridlayout android-recyclerview
我有一个父级Horizontal Recycleview,其中包含十二个Vertical Recycleviews。我想每页仅显示两列,仅在一个水平行上。我用GridLayoutManager。
GridLayoutManager layoutManager = new GridLayoutManager(this,1);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
Run Code Online (Sandbox Code Playgroud)
增加跨度会2创建我不想要的两行。
我还尝试layout:width在 xml 中固定值dpor ,sp这会导致不同屏幕尺寸上的列数不同。
每个块的xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_table_view_game_block_constraint_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_round_top"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/textView_table_view_odds_block_time"
android:layout_width="0dp"
android:layout_height="50sp"
android:gravity="center_vertical"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_table_view_odds_block_hometeam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:textAlignment="textStart"
android:background="@android:color/transparent"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_table_view_odds_block_time" />
<Button
android:id="@+id/button_table_view_odds_block_drawable"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_hometeam" />
<Button
android:id="@+id/button_table_view_game_odds_awayteam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="@android:color/transparent"
android:textAlignment="textStart"
android:textColor="@color/colorFontWhite"
android:textSize="@dimen/font_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_table_view_odds_block_drawable" />
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
根据屏幕尺寸动态设置宽度。这是获得完美结果的最佳方法。
像这样更新 Horizontal Recyclerview 的 BindViewHolder 。
@Override
public void onBindViewHolder(@NonNull UsersAdapter.UsersAdapterVh holder, int position) {
UserModel userModel = userModelList.get(position);
String username = userModel.getUserName();
String prefix = userModel.getUserName().substring(0,1);
holder.tvUsername.setText(username);
holder.tvPrefix.setText(prefix);
holder.item_layout.getLayoutParams().width = getScreenWidth(context)/2;
}
public static int getScreenWidth(Context context) {
WindowManager wm= (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
Run Code Online (Sandbox Code Playgroud)
这就是给定示例中 RecyclerView 项目布局的样子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:padding="10dp"
android:id="@+id/holder_view"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="50dp"
android:background="@drawable/users_bg"
android:layout_height="50dp">
<TextView
android:id="@+id/prefix"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="T"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="@color/headerColor"
android:text="username"
android:layout_marginStart="90dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView"
android:layout_margin="10dp"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_navigate_next_black_24dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6286 次 |
| 最近记录: |