gpt4 book ai didi

java - LinearLayout 不测量内部的 TextView

转载 作者:行者123 更新时间:2023-12-02 02:30:33 25 4
gpt4 key购买 nike

我正在尝试使用 GridLayoutManager 实现一个 RecyclerView ,其中包含方形 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采用整行布局并且文本没有显示。 enter image description here如何为 LinearLayout 中的 View 设置优先级或类似的内容?

这是添加 shell dumpsys Activity 顶部 结果:

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}

最佳答案

    <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 ImageView 类是这样的

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);
}

}

关于java - LinearLayout 不测量内部的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com