我正在尝试创建一个简单的Android聊天应用。 我需要做的是在聊天文本周围创建一个聊天气泡。此气泡的宽度不应大于屏幕宽度的85%,即使它包含更多文本(文本应移动到下一行)。但是,如果气泡中的文本量较少,则气泡的宽度应该包裹内容。
原因是我在气泡右侧显示聊天时间,如果气泡宽度超过屏幕大小的85%,则日期将不可见。
以下是布局文件:
<TextView
android:id="@+id/chatBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat Text"
android:textColor="#222222"
android:textSize="19sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/chatDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="Date"
android:textColor="#999999"
android:textSize="12sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"/>
</LinearLayout>
答案 0 :(得分:0)
只能用Java代码制作它。在您的活动中:
private TextView yourBubbleTextView;
在onCreate()
:
yourBubbleTextView = (TextView) findViewById(R.id.yourBubbleTvId);
// paddings is the total amount of paddings which surround the bubble
// i.e. if you lave LinearLayout padding="5" then use 10 (5 left 5 right)
int totalSize = getWindoManager().getDefaultDisplay().getWidth() - paddings;
// if you aren't using LinearLayout, replace it with whatever layout manager you are using
yourBubbleTextView.setLayoutParams(new LinearLayout.LayoutParams(totalSize, ViewGroup.LayoutParams.WRAP_CONTENT));
希望我帮忙!
答案 1 :(得分:0)
如果您已在LinearLayout中显示此内容,则可以利用layout_weight。
所以你的代码最终看起来像这样:
<TextView
android:id="@+id/chatBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Chat Text"
android:textColor="#222222"
android:textSize="19sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/chatDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="Date"
android:textColor="#999999"
android:textSize="12sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"/>
</LinearLayout>
因此,假设您的LinearLayout占据了屏幕的整个宽度(使用fill_parent
或match_parent
),chatBody现在将是宽度的80%,chatDate将是宽度的20%。如果你想要它是85%你可以做17和3的重量,甚至85和15,如果你想要精确。
答案 2 :(得分:0)
我所做的就是将第一个TextView的宽度设置为&#34; 0dp&#34;。 然后我给它一个大于0的layout_weight。
这里有适合我的代码。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="left"
android:orientation="horizontal"
android:background="@drawable/chat_bubble"
android:padding="10dp">
<TextView
android:id="@+id/chatBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Chat Text is a lot of long text that should ideally move to the next line automatically"
android:textColor="#222222"
android:layout_weight="0.1"
android:textSize="19sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/chatDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:gravity="right"
android:text="Date"
android:textColor="#999999"
android:textSize="12sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"/>
</LinearLayout>