如何将视图添加到android抽屉的底部

时间:2014-05-20 09:13:21

标签: android navigation-drawer

在为Android应用设计UI时,我遇到了以下问题。我想将抽屉的ListView中的项目与底部对齐。我是android开发的新手。任何帮助将不胜感激。

这是我的抽屉代码

    <android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0E0E0" >

    <!-- Your main screen -->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvLocation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="Location: "
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/tvLocInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Rajeev Chowk, Block A, Connaught Place"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="Time: "
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/tvTimeInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="9:30 PM"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.79"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/ivMap"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="10dp"
                android:layout_weight="0.85"
                android:scaleType="fitXY"
                android:src="@drawable/map" />

            <Button
                android:id="@+id/btnPanic"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.1"
                android:background="@drawable/red_button"
                android:text="Panic Button"
                android:textColor="#FFFFFF" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_weight="0.010"
            android:background="@drawable/top_border" >
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp" >

            <TextView
                android:id="@+id/tvDeviceStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="Device Status: Working"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvDeviceStatus1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvDeviceStatus"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:text="Battery: 91%(~11 months)"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>

    <!-- left drawer -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#E0E0E0"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />

    </android.support.v4.widget.DrawerLayout>

这是我在ListView

中充气的三个xml文件

1)profile_view.xml                  

    <ImageView
        android:id="@+id/ivProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:src="@drawable/sneha_lata" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sneha Lata"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvEdit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Edit Profile"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="" />

            <ImageView
                android:id="@+id/ivForward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/forward" />
        </LinearLayout>
    </LinearLayout>

    </LinearLayout>

2)emergency_view.xml

    <?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"
    android:paddingBottom="20dp"
    android:paddingTop="20dp" >

    <TextView
        android:id="@+id/tvManage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:text="Manage Emergency Contacts"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/ivForward"
        android:layout_toRightOf="@id/tvManage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/forward" />

    <TextView
        android:id="@+id/tvManageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvManage"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@id/tvManage"
        android:layout_marginLeft="40dp"
        android:fontFamily="sans-serif-light"
        android:text="@string/tvManageText"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

3) logout view
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/logout_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <Button
        android:id="@+id/btnLogout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/red_button"
        android:text="Log Out"
        android:textColor="#FFFFFF" />

</RelativeLayout>

即使我在我的logout_view.xml中使用了android:layout_alignParentBottom =“true”,它仍然没有在抽屉的底部对齐,但它在logout_view.xml中

这是DrawerAdapter的代码

package com.majhe.android.adapter;
import com.majhe.android.LoginActivity;
import com.majhe.android.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;

public class DrawerAdapter extends ArrayAdapter<String> implements OnClickListener {

Context context;
Button btnLogout;

public DrawerAdapter(Context context) {
    super(context, android.R.layout.simple_list_item_1, new String[] {
            "Profile View", "Emergency View", "Logout View" });
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    switch (position) {
    case 0:
        convertView = inflater.inflate(R.layout.profile_view, null, true);
        return convertView;
    case 1:
        convertView = inflater.inflate(R.layout.emergency_view, null,true);
        return convertView;
    case 2:
        convertView = inflater.inflate(R.layout.logout_view, null, true);
        btnLogout=(Button)convertView.findViewById(R.id.btnLogout);
        btnLogout.setOnClickListener(this);
        return convertView;
    }
    return super.getView(position, convertView, parent);
}

@Override
public void onClick(View v) {
    context.startActivity(new Intent(context,LoginActivity.class));
    ((Activity)context).finish();
    }
}

1 个答案:

答案 0 :(得分:1)

如果我是对的,我认为你根本不需要使用ListView?用这样的东西替换你的列表视图可能有帮助

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:orientation="vertical"
android:paddingEnd="10dp" android:gravity="bottom">
<include layout="@layout/profile_view"/>
<include layout="@layout/emergency_view"/>
<include layout="@layout/logout_view"/>
</LinearLayout>

将{d}和layout_height layout_weight设置为视图中的1以填充剩余空间(最有可能是emergency_view),以便您的logot_view将alwyas粘贴到底部