【问题标题】:Android - Add a button outside LinearLayoutAndroid - 在 LinearLayout 之外添加一个按钮
【发布时间】:2014-10-16 18:54:31
【问题描述】:

如何在此 LinearLayout 之外添加按钮?

我需要它在底部。我自己尝试了几种方法,但它在每一行中添加了相同的按钮!非常感谢您的帮助,谢谢!

<?xml version="1.0" encoding="utf-8"?>

<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="wrap_content"
    android:paddingLeft="5dp" >
<CheckBox
    android:id="@+id/checkBox_alarm_active"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:padding="5dp" />
<View
    android:id="@+id/view1"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_marginRight="10dp"
    android:background="#CCCCCC"
    android:paddingRight="10dp" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >
<TextView
    android:id="@+id/textView_alarm_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/textView_alarm_days"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>

编辑请注意:我看到了其他类似的问题,但问题对我来说有点不同,所以这些不起作用。

【问题讨论】:

    标签: android xml android-layout button android-linearlayout


    【解决方案1】:

    如果您希望它位于布局的确切底部视图,请执行此操作

    <RelativeLayout 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"
       tools:context=".MainActivity" >
    
       <LinearLayout
           android:id="@+id/linearlayot"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:paddingLeft="5dp" >
    
        <CheckBox
            android:id="@+id/checkBox_alarm_active"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:padding="5dp" />
    
        <View
            android:id="@+id/view1"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:background="#CCCCCC"
            android:paddingRight="10dp" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" >
    
            <TextView
                android:id="@+id/textView_alarm_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <TextView
                android:id="@+id/textView_alarm_days"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall" />
         </LinearLayout>
      </LinearLayout>
    
       <Button
         android:id="@+id/buttontoadd"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/linearlayot"
         android:layout_centerHorizontal="true"
         android:text="Button" />
    
    </RelativeLayout>
    

    但如果您希望它位于父级底部的极端底部视图中,请将其更改为此..(更改 id="@+id/buttontoadd")

     <Button
         android:id="@+id/buttontoadd"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:text="Button" />
    

    保持其立场..让我知道是否有帮助....

    编辑:改变这个

      <LinearLayout
           android:id="@+id/linearlayot"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:paddingLeft="5dp" >
    

      <LinearLayout
           android:id="@+id/linearlayot"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:paddingLeft="5dp" >
    

    【讨论】:

    • 谢谢!解决了我的问题!
    • 等等,我错了。它使用 Eclipse 显示它应该在图形布局中的位置。但是我在真实设备上遇到了同样的问题o.O
    • 那是因为线性布局是 wrap_content 环绕其子视图-(不是您的子视图不会占用太多空间),更改它并使其成为 match_parent 它将完美地放置在您的设备上-(这是因为 match_parent 使您的视图与设备大小比例相匹配)检查我的编辑,现在@AnonymousMouse
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多