2016-07-20 36 views
1

我有一個LinearLayout與用戶編輯他的配置文件一樣的聯繫表單。然而,無論我給layout_weight設置什麼樣的大小,editText的大小都不會改變。 Tyvm尋求幫助!LinearLayout中的Layout_weight與滾動視圖

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/first_grey" 
     android:orientation="vertical" 
     tools:ignore="ContentDescription"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/toolbar_all_activities" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/viewUploadPicture" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="25" 
       android:layout_marginTop="20dp" 
       android:clickable="true" 
       android:orientation="horizontal"> 

       <de.hdodenhof.circleimageview.CircleImageView 
        android:id="@+id/imgUserProfile" 
        android:layout_width="0dp" 
        android:layout_height="90dp" 
        android:layout_gravity="center_horizontal" 
        android:layout_weight="0.5" 
        android:src="@drawable/skate_boarder" /> 

       <TextView 
        android:layout_width="0dp" 
        android:layout_height="80dp" 
        android:layout_marginEnd="15dp" 
        android:layout_marginRight="15dp" 
        android:layout_weight="0.5" 
        android:gravity="center_vertical|start" 
        android:text="@string/user.edit.upload_photo" 
        android:textColor="@color/fourth_grey" 
        android:textSize="18sp" /> 

      </LinearLayout> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.first_name" /> 

      <EditText 
       android:id="@+id/txtEditProfileFirstName" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.last_name" /> 

      <EditText android:id="@+id/txtEditProfileLastName" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.favourite_hobby" /> 

      <EditText 
       android:id="@+id/txtEditProfileFavouriteHobby" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.birthday_date" /> 

      <LinearLayout 
       android:id="@+id/birthdayLayout" 
       android:layout_width="300dp" 
       android:layout_height="0dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       android:layout_marginTop="10dp" 
       android:layout_weight="50" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/txtDayBirthdayDate" 
        style="@style/editTextNumberRoundGrey" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="0.2" /> 

       <EditText 
        android:id="@+id/txtMonthBirthdayDate" 
        style="@style/editTextNumberRoundGrey" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_weight="0.2" /> 

       <EditText 
        android:id="@+id/txtYearBirthdayDate" 
        style="@style/editTextNumberRoundGrey" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_weight="0.5" /> 

      </LinearLayout> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.bio" /> 

      <EditText 
       style="@style/editTextRoundGreyBigger" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="120" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.city" /> 

      <EditText 
       android:id="@+id/txtEditProfileCity" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.languages" /> 

      <EditText 
       android:id="@+id/txtEditProfileLanguages" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <TextView 
       style="@style/editProfileTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_weight="40" 
       android:text="@string/user.edit.password" /> 

      <EditText 
       android:id="@+id/txtEditProfilePass" 
       style="@style/editTextRoundGrey" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="50" /> 

      <Button 
       android:id="@+id/btnSubmitChangedData" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       android:layout_marginTop="15dp" 
       android:layout_weight="100" 
       android:padding="10dp" 
       android:background="@color/red" 
       android:text="@string/user.edit.submit" /> 

     </LinearLayout> 

    </ScrollView> 
</LinearLayout> 

回答

0

爲了改變EDITTEXT高度,你也應該更喜歡使用屬性,如maxHeight了minHeightMAXLINESinputTypes,並textAppreance屬性,

這下面的例子可能會幫助你:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:ignore="ContentDescription"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_marginTop="20dp" 
      android:paddingLeft="5dip" 
      android:paddingRight="5dip" 
      > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="FirstName" /> 

      <!-- Person Name EditText--> 

      <EditText 
       android:id="@+id/txtEditProfileFirstName" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="textPersonName" 
       android:maxLines="1" 
       android:maxLength="25" 
       android:textAppearance="?android:attr/textAppearanceMedium"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="LastName" /> 

      <EditText android:id="@+id/txtEditProfileLastName" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="textPersonName" 
       android:maxLines="1" 
       android:maxLength="25" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="Hobby" /> 

      <EditText 
       android:id="@+id/txtEditProfileFavouriteHobby" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="text" 
       android:maxLines="2" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="Birthday Date" /> 

      <LinearLayout 
       android:id="@+id/birthdayLayout" 
       android:layout_width="300dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       android:layout_marginTop="10dp" 
       android:weightSum="1" 
       android:orientation="horizontal"> 
`enter code here`    <EditText 
        android:id="@+id/txtDayBirthdayDate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.3" 
        android:inputType="number" 
        android:gravity="center" 
        android:layout_gravity="center" 
        android:maxLength="2" 
        /> 

       <EditText 
        android:id="@+id/txtMonthBirthdayDate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_weight="0.3" 
        android:gravity="center" 
        android:layout_gravity="center" 
        android:inputType="number" 
        android:maxLength="2" 
        /> 
       <EditText 
        android:id="@+id/txtYearBirthdayDate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_weight="0.4" 
        android:inputType="number" 
        android:gravity="center" 
        android:layout_gravity="center" 
        android:maxLength="4" 
        /> 
      </LinearLayout> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="Bio" /> 

      <!--MultiLine EdiText--> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="textMultiLine" 
       android:minHeight="80dp" 
       android:maxLines="4" 
       android:isScrollContainer="true"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="City" 
       /> 
      <EditText 
       android:id="@+id/txtEditProfileCity" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="textPostalAddress" 
       android:maxLines="1" 
       android:maxLength="25" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="Languages" /> 
      <EditText 
       android:id="@+id/txtEditProfileLanguages" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="text" 
       android:maxLines="1" 
       android:maxLength="25" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:text="password" /> 
      <!--Password EditText--> 
      <EditText 
       android:id="@+id/txtEditProfilePass" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxHeight="40dip" 
       android:inputType="textPassword" 
       android:maxLines="1" 
       android:maxLength="25" 
       android:textAppearance="?android:attr/textAppearanceMedium"/> 

      <Button 
       android:id="@+id/btnSubmitChangedData" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp" 
       android:layout_marginTop="15dp" 
       android:layout_weight="1" 
       android:padding="10dp" 
       android:background="@color/red" 
       android:text="Submit" /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
+0

我已經嘗試過,但它保持不變 –

+0

@AntónioVieira,你有什麼嘗試?看我下面的例子。你必須使用'android:weightSum'屬性才能使'android:layout_weight'有效果。 – Vinnie

+0

@AntónioVieira我編輯了我的答案。請仔細閱讀示例代碼.. – Sathiamour

0

android:layout_weight屬性與android:weightSum屬性結合使用。您的子女layout_weight s必須加總到他們父母佈局的weightSum

您在xml代碼中多次使用layout_weight而未聲明weightSum沒有android:weightSumandroid:layout_weight s將不起作用。舉例來說,如果你想有一個家長的LinearLayout內兩種觀點跨越均勻,你可以做這樣的事情:

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

    <EditText 
     android:id="@+id/my_edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50" 
     android:hint="type something"> 

    <Button 
     android:id="@+id/my_button" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50" 
     android:text="a button"> 
</LinearLayout> 

請注意,您的孩子的意見layout_height旨意需要設置爲"0dp"垂直的LinearLayout方向。對於水平方向,您可以將子視圖'layout_width s設置爲"0dp"

另一個例子,請參閱https://stackoverflow.com/a/7452788/4138919

接受的答案我也注意到你用非浮點值layout_weight小號混合浮點值。出於可讀性目的,考慮堅持一個或另一個。

+0

我已經在其他佈局中做了一些測試,在我提到的問題和'weightSum'它只適用於我,如果我給父定義一個特定的大小(' ScrollView'),我指定'weight_sum'('LinearLayout')。如果我這樣做,一切的大小都會好起來的,'EditText'和其他東西。但在這種情況下,它確實必須是'match_parent',因爲它是一個'ScrollView',所以我有點卡住了。 –

相關問題