Android布局混乱

Android布局混乱,android,Android,这是我的截图: 这是我的代码: 但是,如果文本HIHIHI更改为HIHIHIHI: 时间将消失,气泡将填满 如何做到即使文本是多行的,时间仍然在气泡的右侧?利用布局权重,它只占用指定的空间,不会覆盖时间文本视图 <LinearLayout android:id="@+id/wrapper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:

这是我的截图:

这是我的代码:

但是,如果文本HIHIHI更改为HIHIHIHI: 时间将消失,气泡将填满


如何做到即使文本是多行的,时间仍然在气泡的右侧?

利用布局权重,它只占用指定的空间,不会覆盖时间文本视图

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:weightsum="1">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"  
        android:layout_weight="0.7"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"       
        android:layout_weight="0.3"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</LinearLayout>

利用布局权重,它将只占用指定的空间,不会覆盖时间文本视图

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:weightsum="1">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"  
        android:layout_weight="0.7"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"       
        android:layout_weight="0.3"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</LinearLayout>

在文本视图中使用android:maxwidth。

在文本视图中使用android:maxwidth。

试试这个

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:weightsum="1">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"  
        android:layout_weight="0.7"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"       
        android:layout_weight="0.3"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</LinearLayout>
<RelativeLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="left">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"           
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:layout_alignParentRight="true"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:layout_toLeftOf="@+id/timeIn" 
        android:layout_alignParentLeft="true" 
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</RelativeLayout>
要处理小文本,还可以根据消息和时间调整messageIn的宽度,以显示消息右侧未对齐

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1.0" >

<LinearLayout
    android:id="@+id/lin_lay1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" >

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHI" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lin_lay2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.1" >

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:padding="5dp"
        android:text="23:23" />
</LinearLayout>

</LinearLayout>
试试这个

<RelativeLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="left">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"           
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:layout_alignParentRight="true"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:layout_toLeftOf="@+id/timeIn" 
        android:layout_alignParentLeft="true" 
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</RelativeLayout>
要处理小文本,还可以根据消息和时间调整messageIn的宽度,以显示消息右侧未对齐

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1.0" >

<LinearLayout
    android:id="@+id/lin_lay1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" >

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHI" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lin_lay2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.1" >

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:padding="5dp"
        android:text="23:23" />
</LinearLayout>

</LinearLayout>

您可以通过两种方式完成:-

您可以使用线性布局并将其android:orientation=horizontal设置为水平,并且需要固定Textview的布局宽度

在代码中用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" 
android:orientation="horizontal">
 <TextView
 android:id="@+id/messageIn"
 android:layout_width="200dp"
 android:layout_height="wrap_content"      
 android:text="HIHIHIHIdsfffffffffffffffffffffffffffffffffffffffffffffHIHI" />
<TextView
 android:id="@+id/timeIn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"  
 android:layout_marginTop="5dp"
 android:text="23:23"/>


 </LinearLayout>

您可以通过两种方式完成:-

您可以使用线性布局并将其android:orientation=horizontal设置为水平,并且需要固定Textview的布局宽度

在代码中用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" 
android:orientation="horizontal">
 <TextView
 android:id="@+id/messageIn"
 android:layout_width="200dp"
 android:layout_height="wrap_content"      
 android:text="HIHIHIHIdsfffffffffffffffffffffffffffffffffffffffffffffHIHI" />
<TextView
 android:id="@+id/timeIn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"  
 android:layout_marginTop="5dp"
 android:text="23:23"/>


 </LinearLayout>

解决方案是在TextView中设置Message的android:maxWidth=属性。在运行时-获取设备屏幕的宽度、timeIn TextView的宽度,并使用TextView的setMaxWidthint maxpixels方法设置屏幕宽度-timeIn width之差


解决方案是在TextView中设置Message的android:maxWidth=属性。在运行时-获取设备屏幕的宽度、timeIn TextView的宽度,并使用TextView的setMaxWidthint maxpixels方法设置屏幕宽度-timeIn width之差



这并不是一个真正的解决方案,它将保持文本视图的宽度不变,而且jjLin似乎需要一个可调整大小的文本视图来显示消息哦!那么user936414的答案是正确的,试试他的答案。好的,那么有一件事可以做到。将文本ViewHi设置为线性布局,并为该线性布局赋予0.7权重。它将固定linearlayout的宽度,如果其中的textview将扩展,则可以扩展到它所在的linearlayout的宽度。不,它不正确-它在两个对象之间留有间隙,因此您应该为Edittext指定最大宽度。根据时间宽度textview。这不是一个真正的解决方案,它将保持textview的宽度不变,jjLin似乎需要一个可调整大小的textview来显示消息哦!那么user936414的答案是正确的,试试他的答案。好的,那么有一件事可以做到。将文本ViewHi设置为线性布局,并为该线性布局赋予0.7权重。它将固定linearlayout的宽度,如果其中的textview将扩展,则可以扩展到它所在的linearlayout的宽度。不,它不正确-它在两个对象之间留有间隙,因此您应该为Edittext指定最大宽度。根据时间宽度textview。这是唯一也是最好的解决方案,直到你提供另一个解决方案:这是目前为止唯一的解决方案,我的答案和你的一样,只有更多的精力。这是唯一也是最好的解决方案,直到你提供另一个解决方案:这是目前为止唯一的解决方案,我的答案和你的一样,只有更多的精力用于长文本,但是对于短文本,气泡将有许多空白区域不是解决方案,两个文本视图之间会有间隙,在您的版本中,时间是最右边的,但我希望气泡的时间正好。我尝试搜索有关将权重总和设置为低于布局权重的实际总和的更多信息,但找不到任何内容。你能向我们介绍/详细说明这是如何/为什么起作用的吗?根据Android文档,我假设weightSum应该重置为布局权重的实际总和,但是。。。显然,这并不意味着对长文本的权重和宽度有一个清晰的概念,但对于短文本,气泡将有许多空白区域不是解决方案,两个文本视图之间会有一个间隙,在您的版本中,时间是最右边的,但我希望时间正好合适。我试图搜索更多有关将权重总和设置为低于布局权重实际总和的信息,但什么也找不到。你能向我们介绍/详细说明这是如何/为什么起作用的吗?根据Android文档,我假设weightSum应该重置为布局权重的实际总和,但是。。。显然不是。@ile refere对权重和宽度有一个清晰的概念您没有考虑不同的屏幕大小TextView的宽度是固定的,所以连接不会调整它的大小您没有考虑不同的屏幕大小TextView的宽度是固定的,所以在运行时连接不会调整它的大小,我可以在文本视图中获取时间吗?由于我的timeIn设置为runtimeSorry,我想我不明白这个问题。我的意思是说你在撒谎
ould do:TextView messageIn=TextView findViewByIdR.id.messageIn;TextView timeIn=TextView findViewByIdR.id.timeIn;int-timeInW=timeIn.getMeasuredHeight;Display Display=getWindowManager.getDefaultDisplay;点大小=新点;display.getSizesize;int screenWidth=size.x;messageIn.setMaxWidthscreenWidth-timeInW;或者如果你在运行时为APIIn开发,我可以在textView中获取时间?由于我的timeIn设置为runtimeSorry,我想我不明白这个问题。我的意思是你应该这样做:TextView messageIn=TextView findviewbydr.id.messageIn;TextView timeIn=TextView findViewByIdR.id.timeIn;int-timeInW=timeIn.getMeasuredHeight;Display Display=getWindowManager.getDefaultDisplay;点大小=新点;display.getSizesize;int screenWidth=size.x;messageIn.setMaxWidthscreenWidth-timeInW;或者如果你是为API开发的