2011-02-17 54 views

回答

12

尝试:

layout_width="fill_parent" 
layout_marginLeft="10dip" 
layout_marginRight="10dip" 
+0

感谢那些工作。为什么使用dip而不是px? – Somk 2011-02-17 12:41:07

8

可以设置maragin左,mariagn权,如果你曾经使用相对布局,那么你可以使用下面的参数 layout_centerInParent,机器人的:layout_centerVertical,机器人:layout_centerHorizo​​ntal

如果要将按钮置于垂直居中,则使用居中垂直真,或者如果要将居中按钮置于水平居中,则使用居中水平真 ,如下图所示

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="SignUp Here" 
    android:id="@+id/mysignup" 
    android:layout_alignBottom="@+id/mylogin" 
    android:layout_weight="1" 
    android:layout_centerInParent="true" <!-- use depending upon your need --> 
    android:layout_centerVertical="true" <!-- use depending upon your need --> 
    android:layout_centerHorizontal="true" <!-- use depending upon your need --> 
/> 

倾角是指密度Indepent像素,这意味着,如果您设置的值10,这是相同的所有设备,其中为PX(像素)取绝对值,所以你的定位可能出错的一些设备,这就是为什么,也使的不是,你可以用蘸作为DP太多,编译器转换DP沾

3

第一:忘掉像素,总是使用DP为单位。 你想添加它programaticaly或通过布局的xml文件?

如果您需要添加它programaticaly使用:

LinearLayout layout = new LinearLayout(context); 
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 

    Button button = new Button(context); 
    button.setText("Some label"); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT, 
      1); 
    params.setMargins(10, 0, 10, 0); 
    button.setLayoutParams(params); 

    layout.addView(button); 

如果你想从布局文件中添加不喜欢以下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:paddingTop="3dp" 
android:paddingRight="10dp" android:paddingLeft="10dp" 
android:layout_height="fill_parent" android:layout_width="fill_parent"> 
<Button android:layout_height="fill_parent" android:id="@+id/scroll_story_title" 
    android:ellipsize="end" android:layout_gravity="center" 
    android:maxLines="2" android:gravity="center" 
    android:text="Something to show to the user and that's pretty cool" 
    android:layout_marginTop="3dp" android:textSize="11sp" 
    android:layout_width="fill_parent"></Button>