我试图在RecyclerView
内设GridLayoutManager
,其内部为ImageView
,其下为TextView
。这是我的行布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<com.myCompany.myProject.SquareImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" />
</LinearLayout>
和我的java代码准备RecyclerView
categoryRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.HORIZONTAL, false));
但ImageView
采用整行布局,文字未显示。
如何在LinearLayout
中为视图设置优先级或类似内容?
以下是add shell dumpsys activity top
结果:
android.support.v7.widget.RecyclerView{bd92bdf VFED..... ........ 0,450-1440,1352 #7f09003a app:id/categoryRecyclerView}
android.widget.LinearLayout{44fd02c V.E...... ........ 989,0-1440,451}
com.myCompany.myProject.SquareImageView{5713af5 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{faaf08a V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{5cea6fb V.E...... ........ 989,451-1440,902}
com.myCompany.myProject.SquareImageView{ffc318 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{6e79171 V.ED..... ......ID 193,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{d4f8b56 V.E...... ........ 538,0-989,451}
com.myCompany.myProject.SquareImageView{1d6b7d7 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{bb9f8c4 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{392d3ad V.E...... ........ 538,451-989,902}
com.myCompany.myProject.SquareImageView{6d106e2 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{be4fa73 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{cbfdd30 V.E...... ........ 87,0-538,451}
com.myCompany.myProject.SquareImageView{635fda9 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{1fcaf2e V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{18acacf V.E...... ........ 87,451-538,902}
com.myCompany.myProject.SquareImageView{cfd9c5c V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{8c7cb65 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{228903a V.E...... ........ -364,0-87,451}
com.myCompany.myProject.SquareImageView{e1344eb V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{5f62248 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
android.widget.LinearLayout{3eeb8e1 V.E...... ........ -364,451-87,902}
com.myCompany.myProject.SquareImageView{7ff7606 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
android.support.v7.widget.AppCompatTextView{c5f44c7 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
答案 0 :(得分:1)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<com.myCompany.myProject.SquareImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=1 />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" />
</LinearLayout>
您的Square图像视图类就像这样
public class SquareImageView extends ImageView {
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = getMeasuredHeight();
setMeasuredDimension(height , height);
}
}
答案 1 :(得分:0)
我这样做了
// customrow.xml
<?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="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@android:color/white"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp">
<View
android:layout_width="match_parent"
android:layout_height="20dp"
/>
<ImageView
android:id="@+id/imggridicon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/book"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
/>
<TextView
android:id="@+id/txticdgridmaintitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
android:text="title"
android:textColor="@color/colorPrimary"
android:textSize="13sp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
//布局管理器
GridLayoutManager lLayout = new GridLayoutManager(Activity_Grid_Main_Home.this, 2);
recyclerView = (RecyclerView) findViewById(R.id.mainhome_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(lLayout);
答案 2 :(得分:0)
让我们在LinearLayout
中使用布局权重。它将要做的是设置SquareImageView
的优先级,并且它的大部分内容都是由它来完成的。不要惊讶,但我们会将高度设置为0dp
以提高性能。继续将此属性添加到SquareImageView
。
android:layout_weight="1"
android:layout_height="0dp"