Android - 以编程方式操作ListView下方的按钮位置

时间:2013-07-23 03:39:38

标签: java android android-layout listview

我目前想要遵循这种布局,但不确定如何控制下面的按钮。我的问题是,无论列表视图的宽度如何,图像都会伸展自己。我想按照下面的布局。我正在使用Android的后退按钮。 enter image description here

enter image description here

我不太熟悉以编程方式控制图像的位置及其默认大小。该按钮位于addFooterView(btnLoadMore);

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);

            // Getting listview from xml
            ListView lv = (ListView) rootView.findViewById(android.R.id.list);

            // Creating a button - Load More
            Button btnLoadMore = new Button(mContext);
            btnLoadMore.setBackgroundResource(R.drawable.navigation_back);

            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
            params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
            btnLoadMore.setLayoutParams(params2); 


            // Adding button to listview at footer
            lv.addFooterView(btnLoadMore);

            return rootView;
}

load_main_groups_activty

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"  
                android:background="@color/white"     
                >
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" 

            >
    </ListView>

    <FrameLayout 
    android:id="@+id/contentFragment"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:5)

尝试实施此目的:

          <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"    
                android:background="@color/white"     
                >
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" 
            android:listSelector="@drawable/list_selector">
    </ListView>


         <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentBottom="true"
                    android:background="@null"
                    android:src="@drawable/navigation_back" />

</RelativeLayout>

我相信它会对你有所帮助。感谢

答案 1 :(得分:1)

为了防止图像拉伸,您可以创建LinearLayout并将按钮放在其中。通过设置gravity属性,您可以将按钮放在ListView页脚的中心,左侧或右侧。将按钮添加到LinearLayout后,LinearLayout可以添加到ListView页脚。

查看以下代码是否为您提供了所需的布局:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);

        // Getting listview from xml
        ListView lv = (ListView) rootView.findViewById(android.R.id.list);

        // Creating a button - Load More
        Button btnLoadMore = new Button(mContext);
        btnLoadMore.setBackgroundResource(R.drawable.navigation_back);

        LinearLayout llFooter = new LinearLayout(mContext);


        LinearLayout.LayoutParams paramsBtn = new 
                 LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                     LinearLayout.LayoutParams.WRAP_CONTENT);  
        paramsBtn.gravity = Gravity.LEFT;

        llFooter.addView(btnLoadMore, paramsBtn);   

        //RelativeLayout.LayoutParams params2 = new 
              //RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
                     //RelativeLayout.LayoutParams.MATCH_PARENT);  

        //llFooter.setLayoutParams(params2);

        // Adding button to listview at footer
        lv.addFooterView(llFooter);

        return rootView;

}