【问题标题】:XML — How to get two buttons side by side?XML — 如何让两个按钮并排?
【发布时间】:2012-08-01 15:52:39
【问题描述】:

我试图让两个按钮“运行”和“结果”并排。但是我运气不太好。

我尝试使用RelativeLayout 并在两个按钮上添加layout_weight,但它们似乎只是消失了,并且地图占据了屏幕。

谁能指出我做错了什么以及可以做些什么来解决它?

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


      <TextView
          android:id="@+id/myLocationText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello" />
      <TextView
          android:id="@+id/resultsHomeTest"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="" />


      <Button
          android:id="@+id/btnRun"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"

          android:text="Run" />


      <Button
          android:id="@+id/btnResults"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Results" />

      <TextView
          android:id="@+id/resultsTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text=""
          android:textAppearance="?android:attr/textAppearanceMedium" />


  <com.google.android.maps.MapView
    android:id="@+id/myMapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="02kgJwy0ijcQsVCaCzD9sFv69dOi4gXBEUjIbuQ"

  />

    <!--

    Desktop key: 02kgJwy0ijcTK7fd-wxv7OsPUFEveXTUt16lrRA
    Laptop key : 02kgJwy0ijcQsVCaCzD9sFv69dOi4gXBEUjIbuQ
      -->
</LinearLayout>

【问题讨论】:

    标签: android xml android-linearlayout android-relativelayout


    【解决方案1】:

    你也可以使用,但是 lint 说如果嵌套会影响你的性能

    你可以像下面的代码一样使用

    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:text="Slashdot"
            android:id="@+id/textUser" android:layout_weight="1"
            android:textStyle="bold"/>
        <TextView android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:gravity="right"
            android:layout_weight="1" android:id="@+id/textCreatedAt"
            android:text="10 minutes ago"/> 
    </LinearLayout>
    

    但是如果你像下面的代码一样,lint 会说它会影响你的性能。

    <RelativeLayout>
        <LinearLayout>
            <LinearLayout>
                <ImageView
                android:layout_weight="0.3"/>
            </LinearLayout>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    您也可以使用相对布局,但不要使用权重。如果你像下面的代码那样放置它们,你只需要管理边距。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
    
    
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/button1"
            android:text="Button" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      将两个按钮包裹在一个水平的LinearLayout 内:

      <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:orientation="horizontal">
      
      
          <Button
                    android:id="@+id/btnRun"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
      
                    android:text="Run" />
      
      
                <Button
                    android:id="@+id/btnResults"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Results" />
          </LinearLayout>
      

      【讨论】:

      • @csmit android:orientation="horizo​​ntal" 是默认的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2023-03-06
      • 2011-07-20
      • 2023-03-31
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多