2013-09-23 3 views
0

enter image description hereпросмотреть две кнопки рядом друг с другом

У меня есть две кнопки Вход и сканирование. Я хочу, чтобы эти кнопки были горизонтально, как в одной строке. Я привязал горизонтальный макет. Однако это не сработает. Они должны появляться бок о бок. Мой код выглядит следующим образом.

<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="@drawable/imagesand" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="5dp" 
    /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/imagesand" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="207dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginRight="40dp" 
     android:background="@drawable/loginbg" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="User Name" 
      android:textAppearance="@android:style/TextAppearance.Medium" /> 

     <EditText 
      android:id="@+id/emailid" 
      android:layout_width="156dp" 
      android:layout_height="45dp" 
      android:layout_marginTop="5dp" 
      android:background="@drawable/button" 
      android:hint="User Name" 
      android:inputType="text" 
      android:paddingLeft="10dp" 
      android:textAppearance="@android:style/TextAppearance.Medium" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" 
      android:text="Password" 
      android:textAppearance="@android:style/TextAppearance.Medium" /> 

     <EditText 
      android:id="@+id/password" 
      android:layout_width="156dp" 
      android:layout_height="45dp" 
      android:layout_marginTop="5dp" 
      android:background="@drawable/button" 
      android:hint="Password" 
      android:inputType="textPassword" 
      android:paddingLeft="10dp" 
      android:textAppearance="@android:style/TextAppearance.Medium" /> 

     <Button 
      android:id="@+id/login" 
      android:layout_width="71dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="15dp" 
      android:layout_weight="1" 
      android:background="@drawable/login" 

      android:text="Login" 
      android:textAppearance="@android:style/TextAppearance.Medium" 
      android:textColor="#000000" 
      android:typeface="serif" /> 

     <Button android:id="@+id/scan" 
      and 
      android:layout_width="71dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
       android:layout_marginTop="15dp" 
       android:layout_weight="1" 
       android:background="@drawable/login" 
       android:text="scan"/> 
    </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

Наверняка один из этих ответов помогли вам, почему вы приняли havn't прокомментированы или любой? – Jared

ответ

0
Use this code: 

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="5dp" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/imagesand" 
     android:gravity="center" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="207dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="40dp" 
      android:layout_marginRight="40dp" 
      android:background="@drawable/loginbg" 
      android:gravity="center" 
      android:orientation="vertical" 
      android:layout_alignParentBottom="true" 
      android:paddingLeft="20dp" 
      android:paddingRight="20dp" > 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:text="User Name" 
       android:textAppearance="@android:style/TextAppearance.Medium" /> 

      <EditText 
       android:id="@+id/emailid" 
       android:layout_width="156dp" 
       android:layout_height="45dp" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/button" 
       android:hint="User Name" 
       android:inputType="text" 
       android:paddingLeft="10dp" 
       android:textAppearance="@android:style/TextAppearance.Medium" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="15dp" 
       android:text="Password" 
       android:textAppearance="@android:style/TextAppearance.Medium" /> 

      <EditText 
       android:id="@+id/password" 
       android:layout_width="156dp" 
       android:layout_height="45dp" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/button" 
       android:hint="Password" 
       android:inputType="textPassword" 
       android:paddingLeft="10dp" 
       android:textAppearance="@android:style/TextAppearance.Medium" /> 
     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="horizontal" 
      <Button 
       android:id="@+id/login" 
       android:layout_width="71dp" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_marginTop="15dp" 
       android:layout_weight="1" 
       android:background="@drawable/login" 

       android:text="Login" 
       android:textAppearance="@android:style/TextAppearance.Medium" 
       android:textColor="#000000" 
       android:typeface="serif" /> 

      <Button android:id="@+id/scan" 
       and 
       android:layout_width="71dp" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
        android:layout_marginTop="15dp" 
        android:layout_weight="1" 
        android:background="@drawable/login" 
        android:text="scan"/> 
    </LinearLayout> 
     </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 
0

Вы можете либо обернуть кнопки в горизонтальном LinearLayout, как это:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <Button 
     android:id="@+id/login" 
     android:layout_width="71dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="15dp" 
     android:layout_weight="1" 
     android:background="@drawable/login" 

     android:text="Login" 
     android:textAppearance="@android:style/TextAppearance.Medium" 
     android:textColor="#000000" 
     android:typeface="serif" /> 

    <Button android:id="@+id/scan" 
     android:layout_width="71dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="15dp" 
     android:layout_weight="1" 
     android:background="@drawable/login" 
     android:text="scan"/> 
</LinearLayout> 

или использовать RelativeLayout.

2

Просто поместите обе кнопки в LinearLayout, чья ориентация должна быть horizontal

2

Простое использование этого ..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

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

    <Button 
     android:id="@+id/textView" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|center" 
     android:layout_weight="1" 
     android:text="Login" 
     android:textColor="#000000" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     android:text="Scan" 
     android:textColor="#000000" /> 
</LinearLayout> 
</LinearLayout> 
0

RealtiveLayout является лучшим выбором:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<Button 
android:id="@+id/button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@id/entry" 
android:layout_alignParentLeft="true" 
android:layout_toLeftOf="@+id/space" 
/> 
<TextView 
android:id="@id/space" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true"/> 
<Button 
android:id="@+id/button02" 
android:layout_toRightOf="@id/button01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@id/entry" 
android:layout_alignParentRight="true" 
/> 
</RelativeLayout> 

Похоже, вы хотите две равные кнопок, а не завернутый контент. Я создал центрированную проставку, используя TextView, и сравнил ее с ней. Левая кнопка для родительского левого и правого, Правая кнопка - Левая и правая.

0

Это должно роботи:

Изменить эти,

<Button 
     android:id="@+id/login" 
     android:layout_width="71dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="15dp" 
     android:layout_weight="1" 
     android:background="@drawable/login" 

     android:text="Login" 
     android:textAppearance="@android:style/TextAppearance.Medium" 
     android:textColor="#000000" 
     android:typeface="serif" /> 

    <Button android:id="@+id/scan" 
     and 
     android:layout_width="71dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
      android:layout_marginTop="15dp" 
      android:layout_weight="1" 
      android:background="@drawable/login" 
      android:text="scan"/> 

с этим,

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/login" 
      android:layout_width="71dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="15dp" 
      android:layout_weight="1" 
      android:background="@drawable/login" 
      android:text="Login" 
      android:textAppearance="@android:style/TextAppearance.Medium" 
      android:textColor="#000000" 
      android:typeface="serif" /> 

     <Button 
      android:id="@+id/scan" 
      android:layout_width="71dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="15dp" 
      android:layout_weight="1" 
      android:background="@drawable/login" 
      android:text="scan" /> 
    </LinearLayout> 
Смежные вопросы