简体   繁体   English

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

[英]Android -Programmatically Manipulate Button Position below ListView

I currently wanted to follow this layout but not sure how to control the buttons below. 我目前想遵循这种布局,但不确定如何控制下面的按钮。 My problem is that the image stretched itself whatever the listview's width is. 我的问题是,无论列表视图的宽度如何,图像都会自行拉伸。 I wanted to follow the layout below. 我想按照下面的布局。 I am using Android's back button. 我正在使用Android的后退按钮。 在此处输入图片说明

在此处输入图片说明

I am not quite familiar in programmatically controlling the position of the image and it's default size. 我不太熟悉以编程方式控制图像的位置及其默认大小。 The button is inside addFooterView(btnLoadMore); 该按钮位于addFooterView(btnLoadMore);内部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 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>

Try to implement this: 尝试实现这一点:

          <?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>

I am sure it'll help- you. 我相信它将对您有帮助。 Thanks 谢谢

To keep the image from stretching, you can create a LinearLayout and place the button inside it. 为了防止图像拉伸,可以创建LinearLayout并将按钮放置在其中。 By setting the gravity attribute, you can position the button in the center, left or right of the ListView footer. 通过设置gravity属性,可以将按钮定位在ListView页脚的中央,左侧或右侧。 After adding the button to the LinearLayout , the LinearLayout can be added to ListView footer. 将按钮添加到LinearLayout ,可以将LinearLayout添加到ListView页脚。

See if the following code gets you the layout you want: 查看以下代码是否为您提供所需的布局:

@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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM