2016-03-15 1 views
1

Элементы в этом макете выходят из экрана. Я хотел бы установить размер их на 90% экрана. Я начинаю XML.Мои элементы выходят из экрана, и они не выровнены по вертикали

<LinearLayout 
    android:id="@+id/login_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:weightSum="1.0"> 
    <TextView 
     android:id="@+id/username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8" 
     android:text="Username" 
     android:textColor="#FFFFFF" /> 
    <EditText 
     android:id="@+id/login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/username" 
     android:layout_weight="0.8" 
     android:inputType="text" /> 
    <EditText 
     android:id="@+id/password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/first" 
     android:layout_weight="0.8" 
     android:inputType="textPassword" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/password" 
     android:layout_weight="0.8" 
     android:text="@string/login_button" /> 
</LinearLayout> 

мне нужно поместить элементы внутри линейного расположения в колонке.

ответ

0

Если вы хотите, чтобы они занимали 90% экрана, вам нужно поставить weightSum в 10 и layout-weight каждого элемента в 2.25, чтобы 10% экрана не было бесплатным.

<LinearLayout 
    android:id="@+id/login_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:weightSum="10"> 
    <TextView 
     android:id="@+id/username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2.25" 
     android:text="Username" 
     android:textColor="#FFFFFF" /> 
    <EditText 
     android:id="@+id/login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/username" 
     android:layout_weight="2.25" 
     android:inputType="text" /> 
    <EditText 
     android:id="@+id/password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/first" 
     android:layout_weight="2.25" 
     android:inputType="textPassword" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/password" 
     android:layout_weight="2.25" 
     android:text="@string/login_button" /> 
</LinearLayout> 
0

Вы не можете использовать атрибуты, такие как "layout_centerVertical" и "layout_below" с LinearLayout. Они используются с RelativeLayout. Вы можете достичь макета с помощью этого

<LinearLayout 
    android:id="@+id/login_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="10" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
     android:id="@+id/username" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2.25" 
     android:text="Username" 
     android:textColor="#FFFFFF" /> 
    <EditText 
     android:id="@+id/login" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2.25" 
     android:inputType="text" /> 
    <EditText 
     android:id="@+id/password" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2.25" 
     android:inputType="textPassword" /> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2.25" 
     android:text="@string/login_button" /> 
</LinearLayout> 
0

Прежде всего, fill_parent осуждается. Я предлагаю вам вместо этого использовать match_parent, если вы хотите, чтобы ширина и высота списка охватывали весь экран. Во-вторых, вам не хватает некоторых тегов в LinearLayout. Чтобы увеличить ширину списка до 90% экрана, вы можете сделать это по коду в Управлении, но я предлагаю вам использовать отступы и поля, чтобы оставить немного места по краям, это проще.

Это вы что искали?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    android:id="@+id/login_layout" 
    android:padding="20dp" 
    android:gravity="center" > 
     <TextView 
      android:id="@+id/username" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="Username" 
      android:gravity="center_vertical" 
      android:textColor="#FFFFFF" /> 
     <EditText 
      android:id="@+id/login" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="Login" 
      android:inputType="text" /> 
     <EditText 
      android:id="@+id/password" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:hint="Password" 
      android:inputType="textPassword" /> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:text="Login" /> 
</LinearLayout> 

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

enter image description here

0

Делая это, вы говорите, чтобы LinearLayout распределить элементы внутри с тем же пространством не на 90% по горизонтали в колонке. Если вы хотите это сделать, вы должны установить orientation в вертикальное положение и установить некоторый paddingRight на LinearLayout.

Попробуйте это:

<LinearLayout android:id="@+id/login_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:gravity="right" 
    android:weightSum="1.0" 
    android:paddingRight="20dp"> 
    <TextView android:id="@+id/username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Username" 
    android:textColor="#FFFFFF" 
    /> 
    <EditText 
    android:id="@+id/login" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/username" 
    android:inputType="text" 
    /> 
    <EditText 
    android:id="@+id/password" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/first" 
    android:inputType="textPassword" 
    /> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/password" 
    android:text="@string/login_button" 
    /> 
</LinearLayout> 
Смежные вопросы