English 中文(简体)
如何设置线性布局的布局或边距?
原标题:how to set padding or margin to linear layout?

我有一个线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_menu"
android:layout_width="fill_parent"
android:layout_height="70px"
android:layout_alignParentBottom="true"
android:background="@drawable/footer_bar"
android:gravity="center" >
</LinearLayout>

当我设定条件

if (constant.getscreenresolution() >= 800) {
    //i want to make it height * 2
}

如何设置 layout 参数 ?

最佳回答

希望这能帮助你

LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.fill_parent,
                height * 2);
mLayout = (LinearLayout) findViewById(R.id.layout_menu);
mLayout.setLayoutParams(layout_param);
问题回答

用于铺布 :

LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);
LinearLayout mLayout = (LinearLayout)v.findViewById(R.id.ll_winfowindo);
int left= (int) activity.getResources().getDimension(R.dimen._18sdp);
int top=(int) activity.getResources().getDimension(R.dimen._22sdp);
int right=(int) activity.getResources().getDimension(R.dimen._18sdp);
int bottom=(int) activity.getResources().getDimension(R.dimen._34sdp);
mLayout.setPadding(left, top, right, bottom);

您不应该这样做。 首先您要使用像素( px) 而不是设备独立的像素( dp), 而不是设备独立的像素( dp) 来做这样的事情, 这样创建 UI 只会为特定的设备屏幕配置工作 。 您需要了解 Android 如何解决屏幕密度的变化, 这样您的应用程序就可以独立屏幕密度 。 从这里开始 :

http://developer.android.com/guide/practices/screps_support.html

在线性布局中添加边距 :-

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(30, 20, 30, 0);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签