2017-02-16 36 views
-3

我在創建一些按鈕時遇到了一些小問題。我想在LinearLayout中並排放置2個按鈕(工作)和更多(放置3個按鈕)。其實一切都爲我工作,但如果我把另一個按鈕的按鈕是在下面,但上面有可用空間。LinearLayout(垂直)推杆2並排按鈕和更多按鈕

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" > 


<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

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

     <Button 
      android:id="@+id/button1" 
      android:text="Button1" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginBottom="200dp" /> 

     <Button 
      android:id="@+id/button2" 
      android:text="Button2" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginBottom="100dp" /> 

     <Button 
      android:id="@+id/button3" 
      android:text="Button3" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginBottom="200dp" /> 

    </LinearLayout> 

    </ScrollView> 

    </RelativeLayout> 

這就是它看起來像atm。

image

+0

的問題是在你marginBottom –

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" > 


<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

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

     <Button 
      android:id="@+id/button1" 
      android:text="Button1" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginBottom="200dp" /> 

     <Button 
      android:id="@+id/button2" 
      android:text="Button2" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginBottom="200dp" /> 

     <Button 
      android:id="@+id/button3" 
      android:text="Button3" 
      android:textColor="#FFFFFF" 
      android:textSize="30sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/mybutton" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginBottom="200dp" /> 

    </LinearLayout> 

    </ScrollView> 

    </RelativeLayout> 

更新與這一個你的XML,希望它可以幫助你

+0

你好,我也知道這個方法將其更改爲水平,但與水平我不能把一個按鈕下面的第一個按鈕 – ExiizZ

+0

什麼你需要做什麼?我不明白 –

+0

你需要三個並排的按鈕嗎?還有什麼你需要? –

1

先使每個按鈕0dp的寬度。 第二使在3個按鈕

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" > 


<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

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

    <Button 
     android:id="@+id/button1" 
     android:text="Button1" 
     android:textColor="#FFFFFF" 
     android:textSize="30sp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/mybutton" 
     android:layout_weight="1" 
     android:layout_marginBottom="200dp" /> 

    <Button 
     android:id="@+id/button2" 
     android:text="Button2" 
     android:textColor="#FFFFFF" 
     android:textSize="30sp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/mybutton" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginBottom="200dp" /> 

    <Button 
     android:id="@+id/button3" 
     android:text="Button3" 
     android:textColor="#FFFFFF" 
     android:textSize="30sp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/mybutton" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginBottom="200dp" /> 

</LinearLayout> 

</ScrollView> 

</RelativeLayout> 
+0

+1,如果使用'layout_weight',正確的方法是將寬度設置爲0dp。但'layout_weight'在父佈局中沒有'weightSum'沒有多大意義;) – Shark

0

這裏的保證金底面以下是你的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" > 


    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

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

      <Button 
       android:id="@+id/button1" 
       android:text="Button1" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" 
       android:layout_weight="1" 
       android:layout_marginBottom="200dp" /> 

      <Button 
       android:id="@+id/button2" 
       android:text="Button2" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" 
       android:layout_weight="1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginBottom="200dp" /> 

      <Button 
       android:id="@+id/button3" 
       android:text="Button3" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" 
       android:layout_weight="1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginBottom="200dp" /> 

     </LinearLayout> 

    </ScrollView> 

只是要android:layout_marginBottom="200dp" />在第二個按鈕。

0

發生這種情況是因爲您已將中心引力設置爲LinearLayout

解決方案是唯一的,你只是刪除gravity標記並再次

運行。如果你要放置替換此

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

這個

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

3個按鈕在下面,你應該設置你的李的方向靠近佈局而不是水平線。這將以垂直順序顯示您的按鈕。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" > 


    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

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

      <Button 
       android:id="@+id/button1" 
       android:text="Button1" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" /> 

      <Button 
       android:id="@+id/button2" 
       android:text="Button2" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" /> 

      <Button 
       android:id="@+id/button3" 
       android:text="Button3" 
       android:textColor="#FFFFFF" 
       android:textSize="30sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/mybutton" /> 

     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 
1

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
    <LinearLayout 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="horizontal" 
 
     android:layout_centerHorizontal="true" 
 
     android:id="@+id/layout_one"> 
 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="button 1"/> 
 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="button 2"/> 
 
    </LinearLayout> 
 
    <LinearLayout 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/layout_one" 
 
     android:layout_centerHorizontal="true"> 
 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="button 3" 
 
      /> 
 
    </LinearLayout> 
 
</RelativeLayout>

這是PIC的截圖:

screenshot of pic