如何根据 android 视图中的可见内容调整大小?
Posted
技术标签:
【中文标题】如何根据 android 视图中的可见内容调整大小?【英文标题】:How can I adjust the size deppending on the visible content in an android view? 【发布时间】:2019-09-11 06:05:53 【问题描述】:我正在一个 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_
android:layout_
android:background="@drawable/alarme_layout"
android:backgroundTint="@color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="@+id/textView_aHora"
android:layout_
android:layout_
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_
android:layout_
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="@+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_
android:layout_
android:layout_below="@id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_
android:layout_weight=".9"
android:layout_>
</LinearLayout>
<ImageView
android:id="@+id/imageView_arrowDown"
android:layout_
android:layout_weight=".1"
android:layout_
android:tint="@color/colorOnPrimary"
android:src="@drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/layout_detalhesExpanded"
android:layout_
android:layout_
android:layout_below="@id/textView_aHora">
<CheckBox
android:id="@+id/checkbox_repetir"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_below="@+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/editText_mensagem"
android:layout_
android:layout_weight=".9"
android:layout_
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_
android:layout_weight=".1"
android:layout_
android:tint="@color/colorOnPrimary"
android:src="@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
这是折叠时的项目: https://imgur.com/gallery/dmzw5GU
我希望折叠的项目的大小与内容有关。
【问题讨论】:
【参考方案1】:我在这里猜测您在 XML 布局中设置了静态高度。
您需要将其设置为wrap_content
,这允许动态行高。
【讨论】:
【参考方案2】:我通过将视图的可见性从 View.INVISIBLE
更改为 View.GONE
解决了这个问题。
【讨论】:
以上是关于如何根据 android 视图中的可见内容调整大小?的主要内容,如果未能解决你的问题,请参考以下文章
如何根据应用程序小部件中文本视图中的小部件大小调整文本大小?