android - 如何在 Xamarin Forms 中创建类似聊天的页脚布局?

标签 android xamarin xamarin.android uwp xamarin.forms

我正在使用 Xamarin Forms 为 Abdroid 和 UWP 创建一个类似聊天的应用程序。我想创建一个类似于 this 的页脚布局 (意思是 2 个按钮和一个水平方向的编辑器)。

我使用以下 xaml 在图像中创建了布局:

<Grid >
        <Grid.Margin>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="Android">5,0,5,5</On>
                <On Platform="Windows">5,0,5,25</On>
            </OnPlatform>
        </Grid.Margin>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Editor x:Name="MessageText"  Text="" Grid.Column="0" />
        <local:RoundedButton x:Name="Button2" Clicked="click2"  BackgroundColor="#000000" Grid.Column="1" WidthRequest="50" HeightRequest="50" BorderRadius="25" VerticalOptions="Center" />
        <local:RoundedButton x:Name="Button1" Clicked="click1"  BackgroundColor="#000000" Grid.Column="2" WidthRequest="50" HeightRequest="50" BorderRadius="25" VerticalOptions="Center"/>
</Grid>

此布局的问题是编辑器显示的内容不超过一行。当我继续编写比编辑器宽度更长的文本时,我得到类似 this 的信息.如您所见,第一行被截断,当我开始新行时仅显示它的底部。

据我所知,使用 LinearLayout 和 layout_weight 属性在 Android 中创建这种布局非常容易。但我现在尽量避免为整个页脚创建渲染器。

编辑

这是我在 Android 中编写的示例,具有想要的结果:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  <EditText 
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:hint="Enter a message"/>
  <Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="hi"/>
  <Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="bi"/>
</LinearLayout>

结果如下:

Wanted result

最佳答案

要在文本更改时增大编辑器,您需要触发 InvalidateMeasure强制布局调整大小的方法。这是一个 protected 方法,因此您需要继承 Editor 控件并进行以下更改:

public class CustomEditor : Editor
{      
    public CustomEditor ()     
    {
        TextChanged += (sender, e) => 
        { 
            InvalidateMeasure(); 
        };     
    } 
}

关于android - 如何在 Xamarin Forms 中创建类似聊天的页脚布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790955/

相关文章:

java - 有没有办法将扫描的二维码结果直接发送到服务器?

ios - Xamarin 中的 IBInspectable

c# - 保持 Android 应用程序在后台运行,防止它停止/死亡

visual-studio - Xamarin.Forms Android 应用程序在启动时崩溃(仅在发布版本中)

java - OnPostExecute 永远不会被调用

audio - 将数据按原样发送到 Xamarin.Forms 中的音频设备

android - 在 Viewpager 中滚动 Webview

android - 联系人 API 编辑群组

android - 禁用焦点上的橙色轮廓突出显示

Xamarin Forms 淡出隐藏?