首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓-Programmatically操纵按钮位置低于ListView

安卓-Programmatically操纵按钮位置低于ListView
EN

Stack Overflow用户
提问于 2013-07-23 03:39:38
回答 2查看 1.8K关注 0票数 2

我目前想遵循这个布局,但不确定如何控制下面的按钮。我的问题是,无论listview的宽度是多少,图像都会自我拉伸。我想遵循下面的布局。我正在使用Android的后退按钮。

我不太熟悉以编程方式控制图像的位置和默认大小。按钮在addFooterView(btnLoadMore);内部

代码语言:javascript
复制
        @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

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-23 05:26:03

试着实现这一点:

代码语言:javascript
复制
          <?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>

我相信这会对你有帮助的。谢谢

票数 5
EN

Stack Overflow用户

发布于 2013-07-23 03:59:10

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

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

代码语言:javascript
复制
@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;

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17801107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档