Component with same height

1

Is there a way to make a LinearLayout have the same height as a TextView?

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue"/>

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            app:srcCompat="@drawable/edit"/>


        <ImageView
            android:id="@+id/lista_imagem"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp"
            app:srcCompat="@drawable/avatar"
            android:layout_marginStart="5dp" />

        <TextView
            android:id="@+id/lista_nome"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/lista_imagem"
            android:layout_toRightOf="@+id/lista_imagem"
            android:text="Titulo"
            android:textSize="12sp"
            android:textStyle="bold" />

I wanted the

 android:layout_height=""

From my linear layout it had the same size as the TextView lista_nome , remembering that this should be wrap_content because the text changes size. Is there a way to do this? I know by the way you set a default value, but would you have a way to make this value equal to that of TextView, for example?

Edit: TextView can not stay within this LinearLayout

    
asked by anonymous 07.04.2018 / 17:49

2 answers

2

It is possible to do it in java.

Assign an Id to LinearLayout ( android:id="@+id/linearLayout" ) and in onCreate() add the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...
    ...
    final LinearLayout layout = findViewById(R.id.linearLayout);
    final TextView listaNome = findViewById(R.id.lista_nome);
    ...
    ...

    listaNome.getViewTreeObserver().addOnGlobalLayoutListener(new

         ViewTreeObserver.OnGlobalLayoutListener() {
             @Override
             public void onGlobalLayout() {

                 //Remove o listenner para não ser novamente chamado.
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                     layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                 } else {
                     //noinspection deprecation
                     layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                 }

                 //Obtém os parâmetros do LinearLayout
                 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
                 //Atribui a altura do TextView à altura do LinearLayout
                 layoutParams.height = listaNome.getHeight();
                 layout.setLayoutParams(layoutParams);
             }
         });
}
    
07.04.2018 / 19:01
0

An alternate form without using java:

<LinearLayout
          ...
          android:layout_alignParentTop="true"
          android:layout_alignBottom="@+id/lista_nome"
          .../>
    
07.04.2018 / 19:43