Ada*_*atz 16 xml android android-layout
我想创造这种效果
我正在使用回收站视图,但我的问题是,每张卡都是100%的屏幕宽度,为70%.
这是每个项目的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/scale_20dp">
<LinearLayout
android:id="@+id/button_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/currentYear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/paymentscreengrey"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="35dp"
android:paddingTop="@dimen/scale_50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/scale_20dp"
android:text="**** **** **** 5432"
android:textColor="@color/white"
android:textSize="@dimen/scale_20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2345"
android:textColor="@color/white"
android:textSize="@dimen/scale_16dp" />
Run Code Online (Sandbox Code Playgroud)
Pet*_*rdk 15
我需要将水平 RecyclerView 中的项目设为 recyclerview 宽度的 70%。它可以很容易地完成onCreateViewHolder():
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
view.layoutParams = ViewGroup.LayoutParams((parent.width * 0.7).toInt(),ViewGroup.LayoutParams.MATCH_PARENT)
return ViewHolder(view);
}
Run Code Online (Sandbox Code Playgroud)
bol*_*lot 14
如果要让第一个项目左对齐(即重现屏幕截图中的内容),请子类化LinearLayoutManager并重写这三个generate*LayoutParams方法。这是我的操作方法:https : //gist.github.com/bolot/6f1838d29d5b8a87b5fcadbeb53fb6f0。
class PeekingLinearLayoutManager : LinearLayoutManager {
@JvmOverloads
constructor(context: Context?, @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL, reverseLayout: Boolean = false) : super(context, orientation, reverseLayout)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
override fun generateDefaultLayoutParams() =
scaledLayoutParams(super.generateDefaultLayoutParams())
override fun generateLayoutParams(lp: ViewGroup.LayoutParams?) =
scaledLayoutParams(super.generateLayoutParams(lp))
override fun generateLayoutParams(c: Context?, attrs: AttributeSet?) =
scaledLayoutParams(super.generateLayoutParams(c, attrs))
private fun scaledLayoutParams(layoutParams: RecyclerView.LayoutParams) =
layoutParams.apply {
when(orientation) {
HORIZONTAL -> width = (horizontalSpace * ratio).toInt()
VERTICAL -> height = (verticalSpace * ratio).toInt()
}
}
private val horizontalSpace get() = width - paddingStart - paddingEnd
private val verticalSpace get() = height - paddingTop - paddingBottom
private val ratio = 0.9f // change to 0.7f for 70%
}
Run Code Online (Sandbox Code Playgroud)
该解决方案基于跨线性布局管理器(即,使所有项目适合屏幕)/受其启发 https://gist.github.com/heinrichreimer/39f9d2f9023a184d96f8。
顺便说一句,如果您只想在当前项目居中的情况下向左右显示项目,则可以在“回收者”视图中添加填充并设置clipToPadding为false。在这种情况下,您甚至不需要自定义布局管理器。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">
Run Code Online (Sandbox Code Playgroud)
Gab*_*han 10
有两种方法可以做到这一点.
1)为循环视图使用自定义视图.覆盖onMeasure以将其宽度返回到屏幕的70%.
2)在Recycler View适配器中,创建视图时,将其宽度设置为屏幕宽度的70%.
在任何一种情况下,您都可以从显示屏中获取屏幕尺寸,并将宽度乘以.7.在第一种情况下,您将其设置为EXACT测量宽度,在第二种情况下,您将其设置为布局参数.第二个可能更容易一些.
我有一个类似的问题,我有一个水平RecyclerView的片段,希望每个视图项目的宽度为用户屏幕的三分之一。通过在ViewHolder类的构造函数中添加以下内容来解决此问题:
private class OurViewHolder extends RecyclerView.ViewHolder {
...
public OurViewHolder (LayoutInflater inflater, ViewGroup parent) {
super (inflater.inflate (R.layout.list_item_layout, parent, false));
// This code is used to get the screen dimensions of the user's device
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
// Set the ViewHolder width to be a third of the screen size, and height to wrap content
itemView.setLayoutParams(new RecyclerView.LayoutParams(width/3, RecyclerView.LayoutParams.WRAP_CONTENT));
...
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10844 次 |
| 最近记录: |