【问题标题】:Android studio horizontal layout troublesAndroid studio 横向布局的烦恼
【发布时间】:2018-06-06 18:13:09
【问题描述】:

所以,我正在尝试得到这样的东西。

我在将 +- 按钮排在同一行时遇到了问题。

一旦我有水平布局,我拖动按钮。我结束了这个。我不知道如何使它工作,从代码或设计窗口。请帮忙。

有什么办法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/suma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="171dp"
            android:layout_weight="1"
            android:onClick="incrementaContador"
            android:text="+" />

        <Button
            android:id="@+id/resta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="169dp"
            android:layout_weight="1"
            android:onClick="restaContador"
            android:text="-" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Cafe del dia "
            android:textAlignment="center" />

        <TextView
            android:id="@+id/contadorA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="64dp"
            android:textAlignment="center"
            android:textSize="24sp"
            android:textStyle="bold" />
    </LinearLayout>

    <Button
        android:id="@+id/reinicio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Reinicio"
        android:onClick="reseteaContador" />
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-studio layout


    【解决方案1】:

    我认为你需要这样的东西。将此代码粘贴到您的代码窗口中。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <TextView
            android:id="@+id/titulo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:text="Cafe del dia " />
    
        <TextView
            android:id="@+id/contadorA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/titulo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="4"
            android:textSize="24sp"
            android:textStyle="bold" />
    
        <LinearLayout
            android:id="@+id/button_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/contadorA"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/suma"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_margin="16dp"
                android:onClick="incrementaContador"
                android:text="+"
                android:textSize="30sp" />
    
            <Button
                android:id="@+id/resta"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_margin="16dp"
                android:onClick="restaContador"
                android:text="-"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>
    
        <Button
            android:id="@+id/reinicio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:onClick="reseteaContador"
            android:text="Reinicio" />
    </RelativeLayout>
    

    在我看来,通过编码更容易获得合适的设计。我有这样的事情。

    【讨论】:

      【解决方案2】:

      你有

      android:layout_alignParentTop="true" android:layout_marginTop="171dp"

      一个按钮和

      android:layout_alignParentTop="true" android:layout_marginBottom="169dp"

      另一方面。

      试试这个:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          tools:context=".MainActivity"
          tools:showIn="@layout/activity_main">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentStart="true"
              android:layout_alignParentTop="true"
              android:orientation="horizontal">
      
              <Button
                  android:id="@+id/suma"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_vertical"
                  android:layout_weight="1"
                  android:onClick="incrementaContador"
                  android:text="+" />
      
      
              <Button
                  android:id="@+id/resta"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_vertical"
                  android:layout_weight="1"
                  android:onClick="restaContador"
                  android:text="-" />
      
      
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentStart="true"
              android:layout_alignParentTop="true"
              android:orientation="vertical">
      
              <TextView
                  android:id="@+id/titulo"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="15dp"
                  android:text="Cafe del dia "
                  android:textAlignment="center" />
      
              <TextView
                  android:id="@+id/contadorA"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="64dp"
                  android:textAlignment="center"
                  android:textSize="24sp"
                  android:textStyle="bold" />
          </LinearLayout>
      
          <Button
              android:id="@+id/reinicio"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:text="Reinicio"
              android:onClick="reseteaContador"
              />
      
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        您的嵌套视图太多。您可以只使用垂直线性布局作为父视图组,而水平线性布局布局权重为 1 用于中间两个按钮。

        例如:

        <?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">
        
        <TextView
            android:id="@+id/titulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="Cafe del dia "
            android:textAlignment="center" />
        
        <TextView
            android:id="@+id/contadorA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:textAlignment="center"
            android:textSize="24sp"
            android:text="24"
            android:textStyle="bold" />
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:layout_weight="1">
        
            <Button
                android:id="@+id/suma"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="30dp"
                android:onClick="incrementaContador"
                android:text="+" />
        
            <Button
                android:id="@+id/resta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="restaContador"
                android:text="-" />
        
        </LinearLayout>
        
        <Button
            android:id="@+id/reinicio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Reinicio"
            android:onClick="reseteaContador" />
        
        </LinearLayout>
        

        一旦你学会使用它,最好使用它。

        【讨论】:

          【解决方案4】:

          您应该改为使用 ConstraintLayout 来实现您想要的。它会比使用 RelativeLayout 更有效,尤其是在其中嵌套子布局。使用 ConstraintLayout,您可以使用 GUI 构建器在屏幕上拖动元素并使其像素完美。 ConstraintLayout 可能需要一些工作,但它确实非常强大,当您拥有由许多嵌套 ViewGroup 组成的复杂 Viewgroup 层次结构时,您可以获得显着的性能提升。

          但作为解决您的问题的方法,这里是更新的代码:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          xmlns:app="http://schemas.android.com/apk/res-auto"
                          xmlns:tools="http://schemas.android.com/tools"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          app:layout_behavior="@string/appbar_scrolling_view_behavior"
                          tools:context=".MainActivity"
                          tools:showIn="@layout/activity_main">
          
              <LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:orientation="horizontal">
          
                  <Button
                      android:id="@+id/suma"
                      android:layout_width="75dp"
                      android:layout_height="75dp"
                      android:layout_marginRight="15dp"
                      android:onClick="incrementaContador"
                      android:textSize="35sp"
                      android:text="+" />
          
                  <Button
                      android:id="@+id/resta"
                      android:layout_width="75dp"
                      android:layout_height="75dp"
                      android:layout_marginLeft="15dp"
                      android:onClick="restaContador"
                      android:textSize="35sp"
                      android:text="-" />
          
              </LinearLayout>
          
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:orientation="vertical">
          
                  <TextView
                      android:id="@+id/titulo"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:textSize="30sp"
                      android:text="Contender"
                      android:textAlignment="center" />
          
                  <TextView
                      android:id="@+id/contadorA"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:textAlignment="center"
                      android:textSize="35sp"
                      android:text="4"
                      android:textStyle="bold" />
              </LinearLayout>
          
              <Button
                  android:id="@+id/reinicio"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="15dp"
                  android:layout_centerInParent="true"
                  android:layout_alignParentBottom="true"
                  android:onClick="reseteaContador"
                  android:text="Reinicio"
                  />
          
          </RelativeLayout>
          

          【讨论】:

            【解决方案5】:

            在图片上查看将文本框放在按钮下方的属性。 然后你会得到指示关系的红色圆圈中的波浪线。 如何将它放在页面下方,我仍在努力。 (你可以使用填充,但我相信有更好的解决方案) click

            【讨论】:

            • 哦,在这部分:约束
            猜你喜欢
            • 2016-07-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多