Coloque los botones en la parte inferior de la pantalla con LinearLayout?
Tengo el siguiente código, ¿cómo lo hago para que los 3 botones están en la parte inferior?
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:gravity="center" android:text="@string/observer" android:textAppearance="?android:attr/textAppearanceLarge" tools:context=".asdf" android:weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="1" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="2" /> <Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="3" /> </LinearLayout>
- Cómo crear archivos estáticos (sólo con archivos xml) ExpandableListView en Android
- Android: DOM vs SAX vs análisis XMLPullParser?
- Barra de progreso circular (para un temporizador de cuenta regresiva)
- Línea de archivo XML binario # 26: Error al inflacionar la clase android.widget.TextView
- Ejemplo de programa para android Parche relajante usando xml API
- Marco XML simple en android, atributo de clase
- Cómo diseñar o diseñar correctamente un cajón de navegación de Android
- Android: propósito de usoLevel en etiqueta de forma
- Uso de los colores para el nuevo tema de diseño de material en Android
- El enfoque de EditText es inconsistente en todos los diseños
- Cambiar el color del texto del elemento de menú en el cajón de navegación
- Desmarcando archivos XML en objetos Java en Android?
- Android.view.InflateException: Línea de archivo XML binario # 33: Error al inflar fragmento de clase
Usted necesita asegurar cuatro cosas:
- Tu
LinearLayout
externo tienelayout_height="match_parent"
- Su
LinearLayout
interno tienelayout_weight="1"
ylayout_height="0dp"
- Su
TextView
tienelayout_weight="0"
- Usted ha establecido la gravedad correctamente en su interior
LinearLayout: android:gravity="center|bottom"
Tenga en cuenta que fill_parent
no significa "ocupar todo el espacio disponible". Sin embargo, si usa layout_height="0dp"
con layout_weight="1"
, una vista ocupará todo el espacio disponible ( No se puede obtener el diseño adecuado con "fill_parent" ).
Aquí hay un código que escribí rápidamente que utiliza dos LinearLayouts de una manera similar a su código.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/db1_root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/cow" android:layout_weight="0" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center|bottom" android:orientation="vertical" > <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="1" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="2" /> <Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="145dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center" android:text="3" /> </LinearLayout> </LinearLayout>
El resultado se parece a esto:
Puede usar un RelativeLayout
y alinearlo al fondo con android:layout_alignParentBottom="true"
Crear diseño relativo y dentro de ese diseño crear su botón con esta línea
android:layout_alignParentBottom="true"
Primero cree el nombre del archivo como footer.xml
poner este código dentro de él.
<?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="78dp" android:layout_gravity="bottom" android:gravity="bottom" android:layout_weight=".15" android:orientation="horizontal" android:background="@drawable/actionbar_dark_background_tile" > <ImageView android:id="@+id/lborder" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/overlay" /> <ImageView android:id="@+id/unknown" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/notcolor" /> <ImageView android:id="@+id/open" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/openit" /> <ImageView android:id="@+id/color" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/colored" /> <ImageView android:id="@+id/rborder" android:layout_width="0dp" android:layout_height="wrap_content" android:background="@drawable/action_bar_left_button" android:src="@drawable/frames" android:layout_weight=".14" /> </LinearLayout>
Entonces cree header.xml y ponga este código dentro de él .:
<?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="@dimen/action_bar_height" android:layout_gravity="top" android:baselineAligned="true" android:orientation="horizontal" android:background="@drawable/actionbar_dark_background_tile" > <ImageView android:id="@+id/contact" android:layout_width="37dp" android:layout_height="wrap_content" android:layout_gravity="start" android:layout_weight=".18" android:scaleType="fitCenter" android:background="@drawable/action_bar_left_button" android:src="@drawable/logo"/> <ImageView android:id="@+id/share" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="start" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/share" /> <ImageView android:id="@+id/save" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/save" /> <ImageView android:id="@+id/set" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/set" /> <ImageView android:id="@+id/fix" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/light" /> <ImageView android:id="@+id/rotate" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/ic_menu_rotate" /> <ImageView android:id="@+id/stock" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".14" android:background="@drawable/action_bar_left_button" android:src="@drawable/stock" /> </LinearLayout>
Y luego en su main_activity.xml
y poner este código dentro de él: –
<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="fill_parent" tools:context=".MainActivity" android:id="@+id/relt" android:background="@drawable/background" > <LinearLayout android:layout_width="fill_parent" android:layout_height="78dp" android:id="@+id/down" android:layout_alignParentBottom="true" > <include android:layout_width="fill_parent" android:layout_height="78dp" layout="@layout/footer" > </include> </LinearLayout> <ImageView android:id="@+id/view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/down" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/inc" > </ImageView> <include layout="@layout/header" android:id="@+id/inc" android:layout_width="fill_parent" android:layout_height="50dp"></include>
Feliz codificación
Simplemente añada layout_weight = "1" a su linearLayout con botones.
Edit: – déjame hacerlo simple
Siga algo como abajo, el nombre de las etiquetas puede no ser correcto, es sólo una idea
<LL>// Top Parrent LinearLayout <LL1 height="fill_parent" weight="1" "other tags as requirement"> <TV /><Butons /></LL1> // this layout will fill your screen. <LL2 height="wrap_content" weight="1" orientation="Horizontal" "other tags as requirement"> <BT1 /><BT2/ ></LL2> // this layout gonna take lower part of button height of your screen <LL/> TOP PARENT CLOSED
Añadir android:windowSoftInputMode="adjustPan"
para manifestar – a la actividad correspondiente:
<activity android:name="MyActivity" ... android:windowSoftInputMode="adjustPan" ... </activity>
Puede agrupar los botones dentro de un RelativeLayout, incluso si su diseño principal es lineal. Asegúrese de que el padre más externo tiene el atributo androide: layout_height establecido en match_parent . Y en esa etiqueta Button agrega 'android: alignParentBottom = "True"'
Utilice RelativeLayout:
<RelativeLayout android:id="@+id/bottom_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentBottom="true" android:text="Button2" /> </RelativeLayout>