2015-02-10 67 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