简体   繁体   English

TextView在运行时扩展

[英]TextView expand on Runtime

I have a text box and an ImageView right of this TextVIew. 我有一个文本框和此TextVIew的ImageView右侧。 I make the VISIBILITY of this Image view to GONE if Image URL not available. 如果图像网址不可用,我将使该图像视图的可见性变为“消失”。 Now I want to expand the TextView width to MatchParent. 现在,我想将TextView的宽度扩展到MatchParent。 But its not working. 但是它不起作用。 Something is wrong and I cant figure it out. 出了点问题,我无法解决。 Need help. 需要帮忙。 Code is as under... 代码如下

I tried with all the solutions in StackOverflow but something is really not working. 我尝试了StackOverflow中的所有解决方案,但确实无法正常工作。

Please help. 请帮忙。

                if (ImageURL.equals(null)){
            holder.Image.setVisibility(View.GONE);
//            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
//            holder.question.setLayoutParams();
//            layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
            holder.question.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            holder.question.setLayoutParams(layoutParams);
        }

Try this code and not need set layout param 试试这个代码,不需要设置布局参数

  <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="10" android:text="sadasdasdasdasdasdasdasdasdsadasdasd" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:src="@mipmap/ic_launcher" android:visibility="visible" /> </LinearLayout> 

Constraint layout is more flexible for this situation. 对于这种情况,约束布局更加灵活。 Use of RelativeLayout is not encouraged anymore. 不再鼓励使用RelativeLayout

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/textView"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/imageView"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>

    <ImageView
            app:layout_constraintStart_toEndOf="@+id/textView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            tools:srcCompat="@tools:sample/avatars"
            android:id="@+id/imageView"
            android:layout_alignParentEnd="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

To make the TextView take the entire width simply change the visibility of the ImageView to GONE . 要使TextView占据整个宽度,只需将ImageViewvisibility更改为GONE

imageView.visibility = GONE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM