2013-07-07 1 views
2

В андроидном программировании, как вы меняете высоту макета, чтобы быть наименьшим, он может быть таким, чтобы все элементы отображались на нем.Как изменить макет, чтобы использовать минимальную высоту в программировании для Android?

<?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="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textview_username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/email" /> 

    <EditText 
     android:id="@+id/textinput_username" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textEmailAddress" /> 

    <TextView 
     android:id="@+id/textview_password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/password" /> 

    <EditText 
     android:id="@+id/textinput_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
     <Button 
      android:id="@+id/button_exit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@+string/cancel" /> 

     <Button 
      android:id="@+id/button_login" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@+string/ok" /> 
    </LinearLayout> 

</LinearLayout> 

Это мой код, и когда я запускаю его, появляется много пробелов. Этот макет отображается только в диалоговых окнах.

Спасибо.

EDIT:

я получил высоту быть сведена к минимуму, но кнопка ОК теперь появляется рядом с кнопкой отмены. Я хочу, чтобы это было правильно выровнено.

ответ

1

При использовании кода ниже

android:layout_height="wrap_content" 

вы на самом деле обеспечить макет выглядеть так, как вы хотите на каждой андроид телефон на земле

Для интерференции Android определяет два атрибута: android: layout_margin и android: padding. Атрибут android: layout_margin определяет интервал для контейнера, в то время как android: padding определяет интервал для представления.

android: padding-Определяет интервал содержимого на всех четырех сторонах элемента управления. Чтобы определить дополнение для каждой стороны отдельно, используйте android: paddingLeft, android: paddingRight, android: paddingTop и android: paddingBottom.

андроид: paddingTop-Определяет интервал между содержанием и верхней частью элемента управления

Android: paddingBottom-Определяет интервал между содержимым и нижней частью элемента управления.

android: paddingLeft-Определяет расстояние между содержимым и левой стороной элемента управления.

android: paddingRight - определяет расстояние между содержимым и правой стороной элемента управления.

* Edited код не забудьте изменить название "mainactivity к тому, что ваше имя деятельности имеет" *

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

     <TextView 
    android:id="@+id/textview_username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+string/email" /> 

<EditText 
    android:id="@+id/textinput_username" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textEmailAddress" /> 

<TextView 
    android:id="@+id/textview_password" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+string/password" /> 

<EditText 
    android:id="@+id/textinput_password" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPassword" />  

    <LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 
    <Button 
     android:id="@+id/button_exit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/cancel" /> 

    <Button 
     android:id="@+id/button_login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/ok" /> 
</LinearLayout> 


</RelativeLayout> 

ВАШЕГО ОТВЕТ ПОЛНОСТЬЮ РАБОЧИЙ КОД НИЖЕ

<?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="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textview_username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/email" /> 

    <EditText 
     android:id="@+id/textinput_username" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textEmailAddress" /> 

    <TextView 
     android:id="@+id/textview_password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/password" /> 

    <EditText 
     android:id="@+id/textinput_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="28.05" > 

     <Button 
      android:id="@+id/button_login" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:gravity="center_vertical" 
      android:text="@+string/ok" /> 

     <Button 
      android:id="@+id/button_exit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="@+string/cancel" /> 

    </RelativeLayout> 

</LinearLayout> 
+0

Можете ли вы показать мне новый код? – omega

+0

конкретно скажите мне, что вы хотите ........ вы уже используете андроид: layout_height = "wrap_content" в вашем относительном макете xml-код –

+0

TRY ADDING ... PADDINGS IN YOUR CODE ... ВИДЕТЬ ОТДЕЛЬНЫЙ ОТВЕТ –

-1

Используйте LinearLayout и удалить поля, как это:

<?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="wrap_content" > 

    <TextView 
     android:id="@+id/textview_username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/email" /> 

    <EditText 
     android:id="@+id/textinput_username" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textEmailAddress" /> 

    <TextView 
     android:id="@+id/textview_password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/password" /> 

    <EditText 
     android:id="@+id/textinput_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/button_exit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/cancel" /> 

    <Button 
     android:id="@+id/button_login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+string/ok" /> 

</LinearLayout> 
+0

Когда я попытался это, я получил ошибки, поэтому мне пришлось изменить match_content в wrap_content, тогда все поля ввода и текстовые поля и кнопки все перекрывают друг друга. – omega

+0

На самом деле я добавил, что ориентация горизонтальная, и это помогло. Но теперь кнопка ОК появляется рядом с кнопкой отмены. Я хочу, чтобы кнопка ОК была выровнена по правому краю. Я обновил свой первый пост новым кодом. – omega

0

Добавить андроид: layout_weight = "1" для кнопок отмены и ok, чтобы они отображались один рядом с другим в горизонтальном LinearLayout.

Вы можете также использовать RelativeLayout вместо этого, и что даст вам лучше контролировать, как кнопки раскладывают, вы можете выровнять вправо, влево, сказать, одна с правой стороны, и т.д.

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