【问题标题】:Android linear layout aligning issuesAndroid线性布局对齐问题
【发布时间】:2018-01-22 11:29:07
【问题描述】:

我想做的是如下

我尝试使用如下线性布局

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget33"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF" >
    <TextView android:layout_height="wrap_content" android:text="Eric Lee" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="50px" android:layout_marginTop="10px"></TextView>

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:layout_width="wrap_content" android:src="@drawable/help" android:layout_height="wrap_content" android:id="@+id/imageButton2"  android:background="@null" android:layout_gravity="left"></ImageButton>
 <ImageButton android:layout_width="wrap_content" android:src="@drawable/help" android:layout_height="wrap_content" android:id="@+id/imageButton2"  android:background="@null" android:layout_gravity="top"></ImageButton>

</LinearLayout>
</LinearLayout>

但是,我在将图像向左浮动和向右图像浮动时遇到问题。他们仍然停留在同一个位置

【问题讨论】:

    标签: android android-layout android-xml


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/widget33"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#FFFFFF">
        <RelativeLayout android:layout_height="wrap_content"
            android:id="@+id/relativeLayout1" android:layout_width="match_parent">
            <ImageButton android:layout_width="wrap_content"
                android:id="@+id/imageButton1" android:src="@drawable/icon"
                android:layout_height="wrap_content" android:layout_alignParentLeft="true"></ImageButton>
            <ImageButton android:layout_width="wrap_content"
                android:id="@+id/imageButton2" android:src="@drawable/icon"
                android:layout_height="wrap_content" android:layout_alignParentRight="true"></ImageButton>
        </RelativeLayout>
        <RelativeLayout android:layout_width="match_parent"
            android:id="@+id/RelativeLayout01" android:layout_height="wrap_content">
            <ImageButton android:layout_width="wrap_content"
                android:id="@+id/ImageButton02" android:src="@drawable/icon"
                android:layout_alignParentLeft="true" android:layout_height="wrap_content"></ImageButton>
            <ImageButton android:layout_width="wrap_content"
                android:id="@+id/ImageButton01" android:src="@drawable/icon"
                android:layout_alignParentRight="true" android:layout_height="wrap_content"></ImageButton>
        </RelativeLayout>
        <LinearLayout android:layout_height="wrap_content"
            android:id="@+id/linearLayout1" android:layout_width="match_parent">
            <EditText android:layout_weight="1" android:id="@+id/editText1"
                android:layout_height="wrap_content" android:layout_width="wrap_content">
                <requestFocus></requestFocus>
            </EditText>
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 嗨,谢谢,它解决了我的问题。我可以知道为什么你使用 relativelayout 工作但 linearlayout 不起作用?
    • 相对布局本身具有左右对齐的属性。你可以使用线性布局,你也必须在这种情况下给予权重,但如果你想对齐左右角,比如使用相对布局。
    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="#FFFFFF"
        android:gravity="center" >
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:gravity="center"
            android:layout_margin="10dp">
    
            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" 
                android:layout_margin="10dp"/>
    
            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" 
                android:layout_margin="10dp"/>
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_margin="10dp" 
            android:gravity="center">
    
            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" 
                android:layout_margin="10dp" />
    
            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" 
                android:layout_margin="10dp" />
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:gravity="center"
            android:layout_margin="10dp" >
    
            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" 
                android:layout_margin="10dp" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个..

            <?xml version="1.0" encoding="utf-8"?>
           <RelativeLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_vertical|center_horizontal">
           <Button 
           android:id="@+id/Button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Schedules"/>
      
           <Button 
            android:id="@+id/Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Button1"
            android:text="Grand Nite"
          />
      
         <Button
         android:id="@+id/Button3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@id/Button1"
         android:text="The paper"/>
         <Button
          android:id="@+id/Button4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_toRightOf="@id/Button3"
          android:layout_below="@id/Button1"
          android:text="The Splash"
         />  
      
        <Button
        android:id="@+id/Button5"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@id/Button3"
         android:text="Facebook" 
         android:layout_alignRight="@+id/Button4"
         android:layout_alignParentLeft="true"/>
      
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 2014-01-12
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 2014-10-24
        • 2011-12-02
        相关资源
        最近更新 更多