android - Nexus 7 编译时没有出现按钮

标签 android button

我正在编译并运行到 Nexus 7 设备而不是模拟器,因为模拟器在我的 macbook 上运行速度非常慢。

这是我的代码:

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/hello" />

   <Button 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button_send"/>

</LinearLayout>

我正在看一本书,但我不知道自己做错了什么。我正在使用 ADT eclipse ide。有任何想法吗?文本在那里,但按钮没有显示,当我在图形 View 中添加一个按钮时,它似乎在注入(inject)代码时起作用。

最佳答案

LinearLayout 在使用 fill_parent 时,一个一个地测量它的每个 child 需要多少大小。因此,在您的示例中,它看到第一个 child 想要拥有它提供给它的所有可用高度 (fill_parent)。

如果你不关心间距

android:layout_height="fill_parent" 更改为 android:layout_height="wrap_content"

假设您希望按钮位于底部

您可以使用 layout_weight 解决这个问题。告诉两个 View 只使用它们需要的空间(即,使用 wrap_content)。接下来将第一个 View 设置为使用 layout_weight=1,以便在决定哪个 View 获得剩余空间时第一个 View 优先。

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello" />

   <Button 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0"
       android:text="Send"/>

</LinearLayout>

结果如下所示,这就是我假设您想要的:

enter image description here

关于android - Nexus 7 编译时没有出现按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665588/

相关文章:

android - 提示用户 Android 应用程序的更新版本

Android 视频播放但 VideoView 没有出现

android - 以编程方式查找是否允许从未知来源安装

android - 软键盘进来后,如何保持部分组件固定?

html - 无论宽度如何,按钮/选项卡看起来都一样

Javascript(不是jquery)按钮onclick,函数更改按钮调用函数的类

android - 如何使多个图标叠加仅在 Google Maps MapView 上的某个缩放级别后出现?

image - Sitecore 营销人员模块的按钮图像

javascript - 使用 Javascript 的 SVG 旋转

javascript - 按钮 onclick 事件(硬编码)