Android图形布局不显示切换按钮

时间:2015-02-10 16:11:06

标签: android eclipse togglebutton graphical-layout-editor

我是android编程的新手,我有一个问题。我使用Eclipse并创建了一个Toggle Button,但是在Graphical布局中没有显示任何内容,它向我显示了“渲染期间引发的异常:-1”。我能做什么?我在我的代码下面发布了我用过的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/etCommands"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Type a Command"
    android:password="true" />

<LinearLayout
    android:weightSum="100"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_weight="20"
        android:id="@+id/bResults"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Try Command" />

    <ToggleButton
        android:layout_weight="80"
        android:paddingBottom="10dp"
        android:checked="true"
        android:id="@+id/tbPassword"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="ToggleButton" />
</LinearLayout>

<TextView
    android:id="@+id/tvResults"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="invalid" />

1 个答案:

答案 0 :(得分:0)

使用“layout_weight”的推荐方法是将尺寸设置为“0dp”并设置所需的重量,因此android会调整它的大小:

<ToggleButton
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="80"
    android:text="ToggleButton" />

我不认为这是它崩溃的原因,所以你最好发布整个xml

---- UPDATE ------------------------------------------ ----

我没有得到任何渲染错误,但是当你的按钮宽度设置为“fill_parent”时(你应该使用“match_parent”而不是“fill_parent”),并且水平线性布局中有两个项目,第二个是不可见的。

此外,当使用权重作为预期的动态维度时,请使用“0dp”。

这是您修改的布局。我还交换了重量,这样你的按钮看起来更好(我认为这是一个错误)

<?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="match_parent"
android:orientation="vertical"
android:padding="25dp" >

<EditText
    android:id="@+id/etCommands"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type a Command"
    android:password="true" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/bResults"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="80"
        android:text="Try Command" />

    <ToggleButton
        android:id="@+id/tbPassword"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:checked="true"
        android:paddingBottom="10dp"
        android:text="ToggleButton" />
</LinearLayout>

<TextView
    android:id="@+id/tvResults"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="invalid" />