简体   繁体   中英

TextView out of the Screen

I'm building a chat Application, So I'm displaying bubbles as background of the textView and also an imageView.

To be able to differ messages from users, I hide a imageView (using visibilty.Gone) and change the layout_gravity to right ou left to correctly display the message.

Everything is working great, except when the message from the "Mark Zuckerberg" (and only when it's from him) is too longo, the textview goes out of the screen.

Here is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<br.com.guitcastro.meetapp.views.CircularImageView
    android:id="@+id/leftImageView"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center_vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:visibility="visible" />

<TextView
    android:id="@+id/comment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/bubble_purple"
    android:gravity="center_vertical|center_horizontal"
    android:text="Hello bubbles! This is a very ver"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/primary_text_light" />

<br.com.guitcastro.meetapp.views.CircularImageView
    android:id="@+id/rightImageView"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center_vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
        android:visibility="visible" />
</LinearLayout>

The code to display the "right" layout:

   if (left) {
        rightImageView.setVisibility(View.GONE);
        leftImageView.setVisibility(View.VISIBLE);
        messageText.setTextColor(Color.WHITE);
        this.setImageDrawable(leftImageView, me);

        messageText.setBackgroundResource(R.drawable.bubble_green);
        wrapper.setGravity(Gravity.LEFT);
    } else {
        leftImageView.setVisibility(View.GONE);
        rightImageView.setVisibility(View.VISIBLE);
        this.setImageDrawable(rightImageView, StringUtils.parseName(message.getFrom()));

        messageText.setBackgroundResource(R.drawable.bubble_yellow);
        wrapper.setGravity(Gravity.RIGHT);
    }

Here is a screenShot, what I have for now (I know the bubbles are wrong, please ignore, this isn't important right now).

在此处输入图片说明

maybe this can help you

// get dimensions
Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth(); // largura

// get dimensions in percent
double percentWidth = width/100;

int percentToSetHeight = percentHeight*80;

if (left) {
        rightImageView.setVisibility(View.GONE);
        leftImageView.setVisibility(View.VISIBLE);
        messageText.setTextColor(Color.WHITE);
    messageText.setMaxWidth(percentToSetHeight);
        this.setImageDrawable(leftImageView, me);

        messageText.setBackgroundResource(R.drawable.bubble_green);
        wrapper.setGravity(Gravity.LEFT);
    } else {
        leftImageView.setVisibility(View.GONE);
        rightImageView.setVisibility(View.VISIBLE);
        this.setImageDrawable(rightImageView, StringUtils.parseName(message.getFrom()));
    messageText.setMaxWidth(percentToSetHeight);
        messageText.setBackgroundResource(R.drawable.bubble_yellow);
        wrapper.setGravity(Gravity.RIGHT);
    }
// try this way,hope this will help you...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <br.com.guitcastro.meetapp.views.CircularImageView
        android:id="@+id/leftImageView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:background="@drawable/bubble_purple"
            android:gravity="center"
            android:text="Hello bubbles! This is a very ver"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/primary_text_light" />
    </LinearLayout>


    <br.com.guitcastro.meetapp.views.CircularImageView
        android:id="@+id/rightImageView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:visibility="visible" />
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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