添加填充的观点编程 [英] Add padding on view programmatically
问题描述
我deveoping的Android V2.2应用程序。
I am deveoping Android v2.2 app.
我有一个片段
在我的片段类的 onCreateView(...)
回调,我膨胀的布局片段如下图所示:
In the onCreateView(...)
callback of my fragment class, I inflate an layout to the fragment like below:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login, null);
return view;
}
以上充气布局文件(login.xml):
The above inflated layout file is (login.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username" />
</LinearLayout>
我想设定一个 paddingTop
上述&LT;的LinearLayout&GT;
元素,我想要做的它在的Java code,而不是做它在XML。
I would like to set a paddingTop
to the above <LinearLayout>
element , and I want to do it in the Java code instead of do it in xml.
如何设置 paddingTop
到&LT;的LinearLayout&GT;
在我的片段Java类code ??
How to set paddingTop
to <LinearLayout>
in my fragment Java class code ??
推荐答案
<一个href="http://developer.android.com/reference/android/view/View.html#setPadding%28int,%20int,%20int,%20int%29"><$c$c>view.setPadding(0,padding,0,0);$c$c>
这将设置顶部填充,以填充
-pixels。
This will set the top padding to padding
-pixels.
如果您希望将其设置成 DP
相反,你可以做一个<一个href="http://developer.android.com/guide/practices/screens_support.html#dips-pels">conversion:
If you want to set it in dp
instead, you can do a conversion:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
这篇关于添加填充的观点编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!