2015-02-10 68 views
0

我是新的android编程,我有一个问题。我使用Eclipse,我创建了一个切换按钮,但在图形布局中没有显示任何内容,它向我展示了这个“在渲染期间引发的异常:-1”。我能做什么?我在我的代码下面发布我已经使用过的代码。Android图形布局不显示切换按钮

<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" /> 

+0

你可以发布整个XML? – Dara 2015-02-10 16:25:48

回答

0

使用 “layout_weight” 推荐的方法是设置维度 “0dp”,并设置所需的重量,因此Android会调整它的大小:

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

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

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

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

另外,在为预期的动态尺寸使用权重时使用“0dp”。

这里是你的布局固定。我也交换重量,所以你的按钮看起来更好(我认为这是一个bug)

<?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" /> 
+0

我刚刚添加了整个xml代码 – Diakos 2015-02-10 21:28:58

+0

我的答案现在已更新,请检查它。祝你好运:) – 2015-02-11 11:24:53

+0

谢谢你的努力和你的时间,但我创建了一个普通/白色的xml文件,我从图形布局中拖放切换按钮图标,并向我显示以下消息: “ :-1 在窗口>显示视图>错误日志中记录异常详细信息布局编辑器中的图形预览可能不准确: Path.addRoundRect中不支持不同的边角大小(此会话忽略)“ – Diakos 2015-02-11 18:28:33