0

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="30dp"> 
    <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:orientation="vertical" 
      android:layout_marginTop="@dimen/login_view_margin_top" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/rounded_white_bg"> 
     <EditText 
       android:id="@+id/loginEditText" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:background="@drawable/dialog_edittex" 
       android:layout_margin="10dp"/> 
     <EditText 
       android:id="@+id/passwordEditText" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginBottom="10dp" 
       android:background="@drawable/dialog_edittex"/> 
     <TextView 
       android:id="@+id/forgetPassword" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textColor="@android:color/black" 
       android:layout_gravity="center" 
       android:layout_marginBottom="10dp" 
       android:textSize="@dimen/text_size_choose_order_type"/> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_below="@id/layout1" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:layout_centerHorizontal="true" 
      android:orientation="vertical"> 
     <Button 
       android:id="@+id/loginButton" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:background="@drawable/dialog_button_bg" 
       android:textColor="@color/textColor" 
       android:textSize="@dimen/text_size_choose_order_type"/> 
     <TextView 
       android:id="@+id/register" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:textColor="@android:color/white" 
       android:layout_gravity="center" 
       android:layout_marginTop="15dp" 
       android:textSize="@dimen/text_size_choose_order_type"/> 
    </LinearLayout> 

</RelativeLayout> 

centerHorizontal = истина работает нормально в андроида версии 4.0 и выше, но в 2.3.x это нет. У меня есть margin = 30dp, но marginLeft не работает в 2.3.x. Пожалуйста, не могли бы вы сказать мне, что я сделал не так?

В 4 версии

https://dl.dropboxusercontent.com/u/58583958/Screenshot%20from%202013-11-27%2012%3A29%3A02.png

в 2.3 версии

https://dl.dropboxusercontent.com/u/58583958/Screenshot%20from%202013-11-27%2012%3A29%3A10.png

+0

Его чувак он будет работать в 2.3.x я стараюсь во много раз, так что не Проб может быть другой код имеет вероятностный –

+1

Потому что вы даете match_parent в основной относительной макете удалить эту match_parent и использовать fill_parent вариант. –

+0

Я не понимаю, что я бегу в своих Nexus S и Galaxy Nexus. в Galaxy Nexus прекрасно, но в Nexus S это не так. – user3024382

ответ

0

Ваш LinearLayout покрывает всю ширину родительского RelativeLayout. Это означает, что LinearLayout будет охватывать горизонтальную область, и пространство не будет оставаться в центре горизонтали.

Вы можете использовать wrap_content на LinearLayoutandroid:layout_width.

Если, однако, вы пытаетесь центрировать вид по вертикали (поскольку вы использовали layout_height="wrap_content"), то вместо этого вы должны использовать layout_centerVertical="true".

+0

и почему он отлично работает в версии для Android 4.0>? – user3024382

0
// try this 
i have your requirement using linear layout again let me know if any stuff 
<?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="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:padding="30dp"> 

     <EditText 
      android:id="@+id/loginEditText" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"/> 
     <EditText 
      android:id="@+id/passwordEditText" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:inputType="textPassword" 
      android:layout_marginTop="5dp"/> 
     <TextView 
      android:id="@+id/forgetPassword" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:textColor="@android:color/black" 
      android:layout_marginTop="5dp" /> 

     <Button 
      android:id="@+id/loginButton" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:layout_marginTop="5dp"/> 
     <TextView 
      android:id="@+id/register" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:textColor="@android:color/white" 
      android:layout_marginTop="5dp"/> 

</LinearLayout> 
Смежные вопросы