怎么用java代码写一个线性布局;布局里面有两个按钮是水平的

如题所述

android 使两个按钮水平排列的方法是使用lineLayout线性布局,如下代码:

<?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="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >

 
    <View
        android:layout_width="wrap_content"
        android:layout_height="1.2px"
        android:layout_marginBottom="7dp"
        android:background="@color/white" />

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="79dp"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:layout_margin="10dp" >

        <Button
            android:id="@+id/bt1"
            android:layout_width="fill_parent"
            android:layout_height="26dp"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:text="确认对冲"
            android:textColor="@color/white"
            android:textSize="15dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="26dp"
            android:background="@drawable/shapeyuanjiao"
            android:layout_weight="1"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

运行结果如下:

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-14
<?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="horizontal" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Button" />
</LinearLayout>

本回答被提问者和网友采纳
相似回答