Component with same height

Asked

Viewed 30 times

1

There is a way to make a Linearlayout to 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=""

My linear layout had the same size as Textview lista_nome, remembering that this should be wrap_content because the text changes size. Is there a way to do this? I know that by dimen you define a default value, but there would be a way to make that value equal to Textview, for example?

Edit: Textview cannot be inside this Linearlayout

2 answers

2


It is possible if done in java.

Assign an Id to Linearlayout(android:id="@+id/linearLayout") and in the 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);
             }
         });
}
  • Thank you very much ramaral, it worked here

0

An alternative way without using java:

<LinearLayout
          ...
          android:layout_alignParentTop="true"
          android:layout_alignBottom="@+id/lista_nome"
          .../>
  • This only works in this specific case, where the views are in a Ralativelayout and in that relative position between them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.