2016-03-15 4 views
0

У меня есть этот xml-файл. Я не могу понять, почему мой второй linearlayout, который содержит editText и callButton, не выглядит правильным. Похоже, что это (линия с частью картины телефона):Почему мой layout_weight не работает?

enter image description here

Я в layout_weights установлен на 1 в каждой из этих точек зрения, EditText и callButton, поэтому телефон изображение должно занимать половину экран, правильно? Вот мой код. Я бы хотел, чтобы оба представления занимали одну и ту же ширину - и текст editText не должен растягиваться при вводе текста, как это происходит в настоящее время. Спасибо за любую помощь.

<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="match_parent" 
     android:layout_weight="1"> 
    </ListView> 

<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="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/editText" 
     android:inputType="phone" 
     android:gravity="center" 
     android:background="#d9d9d9" 
     android:layout_weight="1" 

     /> 


     <ImageButton 
      android:layout_width="wrap_content" 
      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="1" 

      /> 
</LinearLayout> 

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

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


     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="wrap_content" 
      android:id="@+id/imageButton" 
      android:src="@drawable/softkeyboard" 
      android:clickable="true" 
      android:onClick="softkeyboardButton" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      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="wrap_content" 
      android:id="@+id/imageButton2" 
      android:src="@drawable/settingsicon" 
      android:background="@drawable/buttonstates" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    </LinearLayout> 
+1

изменить высоту listviews на 0dp – Austi01101110

+1

Также измените ширину изображения и ширину edittext на 0dp – Austi01101110

ответ

2

Изменить свой EditText и кнопку Вызов с layout_width к «0dp» -

<EditText 

андроида: layout_width = "0dp"

 android:layout_height="match_parent" 
     android:id="@+id/editText" 
     android:inputType="phone" 
     android:gravity="center" 
     android:background="#d9d9d9" 
     android:layout_weight="1" 

     /> 


     <ImageButton 

андроида: 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="1" 

      /> 
1

Для layout_weight на работу, вы должны установить либо layout_height или layout_weight быть 0dp на виджетах с весом.

От взглядов вещей, которые я думаю, вы должны потратить немного больше времени, глядя в то, как layout_weight работает точно

https://developer.android.com/guide/topics/ui/layout/linear.html#Weight

1

Попробуйте установить layout_width в EditText и ImageButton к 0dp. Я также рекомендую вам поместить атрибут android: scaleType = "fitCenter" в ImageButton, чтобы изображение помещалось внутри кнопки, и оно не выглядит больше, как на приведенном скриншоте.

Вот 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="match_parent" 
     android:layout_weight="1"> 
    </ListView> 

    <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:hint="Edit Text" 
      android:background="#d9d9d9" 
      android:layout_weight="1" 

      /> 


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

      /> 
    </LinearLayout> 

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

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


     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="wrap_content" 
      android:id="@+id/imageButton" 
      android:src="@android:drawable/ic_menu_gallery" 
      android:clickable="true" 
      android:onClick="softkeyboardButton" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      android:background="@android:drawable/ic_menu_gallery" 
      /> 

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

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="wrap_content" 
      android:id="@+id/imageButton2" 
      android:src="@android:drawable/ic_menu_set_as" 
      android:background="@android:drawable/ic_menu_gallery" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</LinearLayout> 

И вот как это выглядит:

Screenshot

Я надеюсь, что это помогает!

 Смежные вопросы

  • Нет связанных вопросов^_^