2016-03-16 39 views
0

我動態在我的代碼使用更改視圖的寬度寬度相同 - 第一個按鈕比第二個寬。我知道這是因爲一個是浮點數,一個是十進制數,但我怎樣才能使它們都成爲小數點,或者我應該只對浮點數進行反覆試驗?我的params.weight只接受浮點值。layout_weight在浮點和十進制

編輯: 這裏是我的XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context=".MainActivity"> 

    <ListView 
     android:id="@+id/ListView_2" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_weight="1"> 
    </ListView> 
<!--Let's set the height of the linearlayout below to 0dp. Apparently it's less work on resources, 
as otherwise we would be drawing it twice, because the size is also set dynamically--> 
<LinearLayout 
    android:id="@+id/LinearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:orientation="horizontal" 
     > 
     <!--android:adjustViewBounds="true"--> 
    <!-- we want the text to begin in the centre of the edittext view 
    that's what gravity does--> 

    <EditText 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:id="@+id/editText" 
     android:inputType="phone" 
     android:gravity="center" 
     android:background="#d9d9d9" 
     android:layout_weight="4" 

     /> 


     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:id="@+id/callButton" 
      android:src="@drawable/call" 
      android:clickable="true" 
      android:onClick="softkeyboardButton" 
      android:background="#ffa62b" 
      android:layout_weight="2" 

      /> 
</LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:orientation="horizontal" > 

    <CheckBox 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:background="#ffa500" 
     android:text="New CheckBox" 
     android:id="@+id/checkBox" 
     android:layout_weight="5" 
     /> 


     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:id="@+id/imageButton" 
      android:src="@drawable/softkeyboard" 
      android:clickable="true" 
      android:onClick="softkeyboardButton" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1.3" 
      android:background="@drawable/buttonstates" 
      /> 

     <View android:layout_height="fill_parent" 
      android:layout_width="2px" 
      android:background="#90909090"/> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:id="@+id/imageButton2" 
      android:src="@drawable/settingsicon" 
      android:background="@drawable/buttonstates" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1.3" /> 

    </LinearLayout> 

    </LinearLayout> 
+1

如果想法是讓它們匹配'LayoutParams',爲什麼不只是檢索xml中指定的'ImageButton'的'LayoutParams',然後將其設置爲'ImageButton'的'LayoutParams'你想動態?像這樣: 'buttonTwo.setLayoutParams(buttonOne.getLayoutParams());' –

+0

這是一個有趣的想法。回家時會看看它。 – CHarris

+0

讓我知道它是否有效,如果有效,我會將其添加爲答案。 –

回答

1

以建議的形式發佈此答案。 :)

如果這個想法是隻讓他們匹配LayoutParams,爲什麼不直接檢索xml中指出的ImageButtonLayoutParams,然後將其設置爲的ImageButton你要動態的LayoutParams?像這樣:

buttonTwo.setLayoutParams(buttonOne.getLayoutParams());

再次引述@Christophe哈里斯爲其他用戶:

我是有浮動可怕的麻煩 - params.weight = 1.3F。即使我使用setLayoutParams = getLayoutParams,寬度也不會完全相同。可能與漂浮物有關的東西永遠不會準確,我不知道。但後來我將它改爲params.width = 80,確切的說。

0

您需要設置layout_width"0dp"ButtonTwolayout_weight正常工作。

希望這應該適合你!