如何根据android视图中的可见内容调整大小()

出门莫恨无人随,书中车马多如簇。这篇文章主要讲述如何根据android视图中的可见内容调整大小?相关的知识,希望能为你提供帮助。
我正在android应用程序中创建一个列表视图,每个项目都会展开和折叠。
这是每个项目的代码:

< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/alarme_layout" android:backgroundTint="@color/colorPrimaryDark" android:padding="20dp"> < TextView android:id="@+id/textView_aHora" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:text="00:00" android:textSize="26sp" android:textColor="@color/colorOnPrimary" android:layout_marginBottom="10dp"/> < Switch android:id="@+id/switch_aEstado" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" /> < LinearLayout android:id="@+id/layout_ultimaLinhaCollapsed" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textView_aHora" android:layout_alignParentStart="true"> < LinearLayout android:id="@+id/layout_diasRepetir" android:orientation="horizontal" android:layout_width="0px" android:layout_weight=".9" android:layout_height="25dp"> < /LinearLayout> < ImageView android:id="@+id/imageView_arrowDown" android:layout_width="0px" android:layout_weight=".1" android:layout_height="wrap_content" android:tint="@color/colorOnPrimary" android:src="https://www.songbingjia.com/android/@drawable/ic_keyboard_arrow_down_24dp" android:focusable="true" android:clickable="true"/> < /LinearLayout> < RelativeLayout android:id="@+id/layout_detalhesExpanded" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textView_aHora"> < CheckBox android:id="@+id/checkbox_repetir" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="@string/repetir" android:textColor="@color/colorOnPrimary" android:foregroundTint="@color/colorSecondary"/> < LinearLayout android:id="@+id/layout_diasRepetirButtons" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/checkbox_repetir" android:layout_alignStart="@+id/checkbox_repetir" android:layout_marginTop="10dp" android:visibility="invisible"> < /LinearLayout> < LinearLayout android:id="@+id/ultimaLinhaExpanded" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout_diasRepetirButtons" android:layout_marginTop="10dp"> < EditText android:id="@+id/editText_mensagem" android:layout_width="0px" android:layout_weight=".9" android:layout_height="wrap_content" android:hint="@string/mensagemHint" android:inputType="text" android:backgroundTint="@color/colorOnPrimary" android:foregroundTint="@color/colorSecondary" android:textColor="@color/colorSecondary" android:shadowColor="@color/colorSecondary" android:textColorHighlight="@color/colorSecondaryLight" /> < ImageView android:id="@+id/imageView_arrowUp" android:layout_width="0px" android:layout_weight=".1" android:layout_height="wrap_content" android:tint="@color/colorOnPrimary" android:src="https://www.songbingjia.com/android/@drawable/ic_keyboard_arrow_up_24dp" android:focusable="true" android:clickable="true"/> < /LinearLayout> < /RelativeLayout> < /RelativeLayout>

这是列表视图的“getView()”方法:
public View getView(final int position, View convertView, ViewGroup parent) { convertView = getLayoutInflater().inflate(R.layout.alarme, null); final Alarme alarme = alarmes.get(position); //main view TextView hora = convertView.findViewById(R.id.textView_aHora); Switch estado = convertView.findViewById(R.id.switch_aEstado); LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir); final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown); //detalhes final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded); CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir); LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons); EditText editText = convertView.findViewById(R.id.editText_mensagem); final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp); layoutDetalhesExpanded.setVisibility(View.INVISIBLE); hora.setText(alarme.getHora()); estado.setChecked(alarme.isLigado()); cbRepetir.setChecked(alarme.isRepete()); arrowDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { arrowDown.setVisibility(View.INVISIBLE); arrowUp.setVisibility(View.VISIBLE); layoutDetalhesExpanded.setVisibility(View.VISIBLE); alarme.setCollapsed(false); } }); arrowUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { arrowUp.setVisibility(View.INVISIBLE); arrowDown.setVisibility(View.VISIBLE); layoutDetalhesExpanded.setVisibility(View.INVISIBLE); alarme.setCollapsed(true); } }); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Override public void onTextChanged(CharSequence s, int start, int before, int count) {}@Override public void afterTextChanged(Editable s) { alarme.setMensagem(s.toString()); } }); arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE); arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE); layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE); editText.setText(alarme.getMensagem()); return convertView; }

我认为每个项目的大小都会根据可见内容进行包装,但我有这个结果:
这是展开时的项目:https://imgur.com/gallery/oIv50dT
【如何根据android视图中的可见内容调整大小()】这是折叠时的项目:https://imgur.com/gallery/dmzw5GU
我希望折叠的项目的大小包含在内容中。
答案我将在这里猜测,在您的XML布局中,您已设置静态高度。
你需要将它设置为wrap_content,它允许动态行高。
另一答案我通过改变从View.INVISIBLEView.GONE的视图的可见性来解决它。

    推荐阅读