gpt4 book ai didi

android - 屏幕外的TextView

转载 作者:行者123 更新时间:2023-11-29 16:02:05 28 4
gpt4 key购买 nike

我正在构建一个聊天应用程序,所以我将气泡显示为 textView 和 imageView 的背景。

为了能够区分来自用户的消息,我隐藏了一个 imageView(使用 visibilty.Gone)并将 layout_gravity 更改为右侧或左侧以正确显示消息。

一切都很好,除了当来自“马克扎克伯格”的消息(并且只有当它来自他)太长时,textview 会超出屏幕。

这是我的布局:

<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>

显示“正确”布局的代码:

   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);
}

这是我目前拥有的屏幕截图(我知道气泡是错误的,请忽略,这现在不重要)。

enter image description here

最佳答案

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);
}

关于android - 屏幕外的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506663/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com