简体   繁体   English

防止孩子走出屏幕界限

[英]Prevent children from going out of screen bounds

I have a layout with textview and another layout inside it. 我有一个layouttextview和另一个layout里面。 I need the textview as wrap_content but stretchable to parent width without the layout that is next to it going off screen. 我需要textviewwrap_content但可拉伸到父级宽度,而其旁边的布局不会出现在屏幕上。

I tried with relative and LinearLayout and no matter what I do, the message_indicators layout just goes off screen when message_text stretches to match_parent . 我尝试使用relativeLinearLayout ,无论我做什么,当message_text延伸到match_parent时, message_indicators布局都不会显示在屏幕上。 What i want here is that message_text stretches with content and message_indicators need to be right next to it without going off screen when message content increases. 我在这里想要的是message_text包含内容,并且message_indicators必须紧靠其旁边,而当消息内容增加时,不退出屏幕。

Here is my code: 这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10dp" >

<TextView
    android:id="@+id/message_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/message_indicators"
    android:autoLink="all"
    android:background="@drawable/text_bubble_left"
    android:clickable="true"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:text="@string/message_text_hint"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<LinearLayout
    android:id="@+id/message_indicators"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/message_text"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    android:paddingLeft="10dp" >

    <ImageView
        android:id="@+id/message_lock_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_lock"
        android:contentDescription="@string/message_locked_content_description"
        android:paddingBottom="5dp"/>

    <TextView
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/message_time_hint"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/gray" />
</LinearLayout>

</RelativeLayout>

Here's the screenshot. 这是屏幕截图。 Top message represents how it looks like with long messages, middle is short message with current layout and bottom is how it should look like for short messages: 顶部消息代表长消息的外观,中间消息代表当前布局的短消息,底部代表短消息的外观:

I have found a best way to do this. 我找到了一种最佳方法。

Here is the solution. 这是解决方案。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<include
    android:id="@+id/message_section_header"
    layout="@layout/message_section_header" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <View
        android:id="@+id/message_bubble_arrow"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/message_bubble_left_arrow"
        android:rotation="-90" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autoLink="all"
        android:background="@drawable/message_bubble_left"
        android:clickable="true"
        android:padding="10dp"
        android:text="@string/message_text_hint"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />

    <LinearLayout
        android:id="@+id/message_indicators"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minWidth="60dp"
        android:orientation="vertical"
        android:overScrollMode="always"
        android:paddingLeft="10dp" >

        <ImageView
            android:id="@+id/message_lock_indicator"
            style="@style/Layout.Wrap"
            android:contentDescription="@string/message_locked_content_description"
            android:paddingBottom="5dp"
            android:src="@drawable/ic_lock" />

        <TextView
            android:id="@+id/message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/message_time_hint"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/gray" />
    </LinearLayout>
</LinearLayout>

Try android:layout_width="fill_parent" inside the TextView instead. 请尝试在TextView android:layout_width="fill_parent"

If that doesn't work, try moving the TextView block below the LinearLayout block. 如果这不起作用,请尝试将TextView块移至LinearLayout块下方。

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

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