开发者

Problem setting the layout_gravity on a button using LayoutParams

开发者 https://www.devze.com 2023-03-16 02:01 出处:网络
I am adding a button dynamically to a ListActivity. As follows: setContentView(R.layout.listview_singlegrey);

I am adding a button dynamically to a ListActivity. As follows:

setContentView(R.layout.listview_singlegrey);
LayoutInflater inflater = (LayoutInflater)this.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linear = (LinearLayout) findViewById(R.id.list_comment);
Button btAddComment = (Button)inflater.inflate(R.layout.bt_yellow,null);
FrameLayout.LayoutParams lyp1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lyp1.gravity = Gravity.RIGHT;
btAddComment.setLayoutParams(lyp1);     
btAddComment.setText("Añadir comentario");

The LinearLayout for the listview is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:orientation="vertical"
        android:layout_height="fill_parent" android:id="@+id/list_comment"
        android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/fondomain">    
    <ListView android:id="@+id/android:list" android:listSelector="@android:color/transpar开发者_如何学运维ent"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:divider="@layout/linedivider" android:dividerHeight="10px"
          android:cacheColorHint="#0000" android:paddingTop="10dp"           
          />
</LinearLayout>

And the layout of the button from where I am inflating is:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
     style="@style/wrapfill"         
     android:background="@layout/selectoryellowbutton" android:layout_gravity="right" android:padding="10dp"
     android:textSize="16px" android:text="Añadir comentario" android:textStyle="bold" android:textColor="@color/darkergray">
</Button>

As you can see I have tried to set the layout_gravity params in the code and in the button, but none of them works. I still display the button on the left of the screen. It seems that the layout params are not working. But if I set fill_parent for the width I get the button all along the screen and I want the button wrapping the text and align to the right of the screen.


your Button and ListView are contained in LinearLayout, android:layout_gravity attribute should be used with FrameLayout as parent view, don't set FrameLayout.LayoutParams lyp1 as layout parameters for your button,

try instead to set android:gravity="right" of your root LinearLayout or nest your button in another LinearLayout with gravity set to right, you could easily obtain button instance by calling findViewById on inflated layout later

0

精彩评论

暂无评论...
验证码 换一张
取 消