Java 布局权重不工作CardView
我希望我的屏幕显示总共3张均匀分布的卡,占据整个空间,但我尝试的任何东西似乎都不起作用。使用RecyclerView/viewHolder加载这些卡。 我试过两种方法 解决方案1:Java 布局权重不工作CardView,java,android,android-layout,android-cardview,Java,Android,Android Layout,Android Cardview,我希望我的屏幕显示总共3张均匀分布的卡,占据整个空间,但我尝试的任何东西似乎都不起作用。使用RecyclerView/viewHolder加载这些卡。 我试过两种方法 解决方案1: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:c
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
解决方案2:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
有人能帮忙吗
提前谢谢
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weight_sum="3"
android:padding="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 1"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="16dp"
android:background="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 2"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 3"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
</LinearLayout>
我希望这会像您期望的那样起作用:
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="@+id/cardView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#000088"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#008800"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#880000"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
</LinearLayout>
我希望这将如您所期望的那样起作用:
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="@+id/cardView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#000088"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#008800"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#880000"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
</LinearLayout>
我在你的解决方案中看不到三张牌?是否要将您的CardView分成3个等距部分?否,使用viewholder加载卡。抱歉,我是一个新手,但我假设如果我将layoutweight设置为1,加载时,每张卡会在屏幕上占据更大的空间。在这种情况下,GridView会让您的生活更轻松。考虑更改为GRIDVIEW。这也可以在RecyclerView中完成,但只需说一句。感谢您的回复,但我希望我的卡片是全屏宽的,并且垂直放置。Gridview帮不上忙我在你的解决方案中看不到三张卡?是否要将您的CardView分成3个等距部分?否,使用viewholder加载卡。抱歉,我是一个新手,但我假设如果我将layoutweight设置为1,加载时,每张卡会在屏幕上占据更大的空间。在这种情况下,GridView会让您的生活更轻松。考虑更改为GRIDVIEW。这也可以在RecyclerView中完成,但只需说一句。感谢您的回复,但我希望我的卡片是全屏宽的,并且垂直放置。Gridview没有任何帮助我正在使用一个viewholder来加载我需要的信息。如果我按你的方式做,这个能行吗?一个视图持有者?这会在回收站里吗?如果是的话,你应该考虑一下你的设计,因为如果你的适配器上有100个项目,它会很重。是的,它在一个回收视图中,我这样做是因为它总是会返回3个项目。我使用cardView进行样式设计,但不是将卡片均匀分布,而是将一半屏幕留空(我希望我解释得足够清楚)。如果使用recycler视图,布局文件应该只有一张卡片。RecyclerVIew将为您创建其他的。如果需要绑定东西,只要为每个视图指定ID,我的解决方案就可以了。如果您总是要返回三个视图,我想不需要recyclerview,您最好定义一个布局,绑定视图并填充它。只有当您不知道要显示多少项,或者有太多项无法手动创建布局时,RecyclerView才是一种更好的方法。希望这有帮助。谢谢你的建议!我将尝试一下:)我正在使用一个viewholder来加载我需要的信息。如果我按你的方式做,这个能行吗?一个视图持有者?这会在回收站里吗?如果是的话,你应该考虑一下你的设计,因为如果你的适配器上有100个项目,它会很重。是的,它在一个回收视图中,我这样做是因为它总是会返回3个项目。我使用cardView进行样式设计,但不是将卡片均匀分布,而是将一半屏幕留空(我希望我解释得足够清楚)。如果使用recycler视图,布局文件应该只有一张卡片。RecyclerVIew将为您创建其他的。如果需要绑定东西,只要为每个视图指定ID,我的解决方案就可以了。如果您总是要返回三个视图,我想不需要recyclerview,您最好定义一个布局,绑定视图并填充它。只有当您不知道要显示多少项,或者有太多项无法手动创建布局时,RecyclerView才是一种更好的方法。希望这有帮助。谢谢你的建议!我将尝试一下:)我尝试了一下,但是OnClickListener不能用于每个CardViewseparately@SanVed我已经更新了上面的代码。只需为CardView设置id,然后单击Listener即可。“我现在已经测试过了,它对我有好处。”SanVed请再试一次,结果很好。我知道我哪里出错了。我忘了在侦听器中添加视图组。我尝试了一下,但OnClickListener无法为每个CardView工作separately@SanVed我已经更新了上面的代码。只需为CardView设置id,然后单击Listener即可。“我现在已经测试过了,它对我有好处。”SanVed请再试一次,结果很好。我知道我哪里出错了。我忘了在侦听器中添加视图组。