繁体   English   中英

视图在 android 布局中未正确显示

[英]View not showing properly in android layout

我正在 android 中设计自定义对话,为此我将 xml 文件设计为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/item_bg_color"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">

    <TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/custom_dialog_text"
    android:textColor="#000000" 
    android:textSize="14dip"
    android:typeface="serif"
    android:singleLine="false"
    android:text="You are not authorized to use this application." 
    android:layout_marginTop="10dip"/>

    <Button android:id="@+id/custom_button_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="Ok" 
    android:layout_below="@+id/custom_dialog_text"
    android:layout_toRightOf="@+id/custom_dialog_text"/>


</RelativeLayout>

问题:

我想在 TextView 的末尾(右)显示按钮,并且 textview 的文本应该超过一行(如果必须显示更多字符)。 但是当我使用这个布局时,按钮不会出现在任何地方。

我也使用了 LinearLayout 和 TableLayout ,在这两种情况下,按钮都出现了一小部分。 我哪里错了?

基本上你想告诉按钮向右对齐,占用它需要的所有空间,然后将空间的 rest 给 TextView。 为此,您可以为按钮指定android:layout_layout_alignParentRight="true"并为 Z622DC6E792DB10F1064ZCC 指定 android android:layout_toLeftOf="@id/custom_button_ok" 注意顺序!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/item_bg_color" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
  <Button android:id="@+id/custom_button_ok" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"
   android:text="Ok" 
   android:layout_layout_alignParentRight="true" />

  <TextView android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/custom_dialog_text"
   android:textColor="#000000" 
   android:textSize="14dip"
   android:typeface="serif"
   android:singleLine="false"
   android:text="You are not authorized to use this application." 
   android:layout_marginTop="10dip"
   android:layout_toLeftOf="@id/custom_button_ok"
  />
</RelativeLayout>

对于按钮添加属性 android:layout_alignParentRight,android:layout_below,他们帮助你

尝试这样做..

<TextView android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:id="@+id/custom_dialog_text"
    android:textColor="#000000" 
    android:textSize="14dip"
    android:typeface="serif"
    android:singleLine="false"
    android:text="You are not authorized to use this application." 
    android:layout_marginTop="10dip"/>

layout_belw 和 layout_rightof 属性在文本视图中设置为相同的 ID。只需使用 layout_below。还可以将按钮的重力设置为底部

请尝试以下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:weightSum="1.0">
    <RelativeLayout android:layout_height="wrap_content"
        android:layout_width="0dp" android:layout_weight="0.85">
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/custom_dialog_text"
            android:textColor="#FFFFFF" android:textSize="14dip"
            android:typeface="serif" android:singleLine="false"
            android:text="You are not authorized to use zdsaddsad this application."
            android:layout_marginTop="10dip" />
    </RelativeLayout>
    <RelativeLayout android:layout_height="wrap_content"
        android:layout_width="0dp" android:layout_weight="0.15">
        <Button android:id="@+id/custom_button_ok"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Ok" />
    </RelativeLayout>

</LinearLayout>

暂无
暂无

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

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