English 中文(简体)
动态改变安乐斯的线性out或高温
原标题:Dynamically changing the linearlayout width or height on Android
  • 时间:2010-09-29 12:45:06
  •  标签:
  • android

我正试图改变直线布局或任何其他植被,以动态方式,但放弃例外。

我的任务是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/abc"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView   
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ghghjkgj ghjg hjgj ghj g hjgjgh jhg "
    />
</LinearLayout>

以及 我的活动是:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll = (LinearLayout)findViewById(R.id.abc);
        ll.setLayoutParams(new LinearLayout.LayoutParams(30,60));
    }

以下例外:

E/AndroidRuntime(16052): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
E/AndroidRuntime(16052):    at android.widget.FrameLayout.onLayout(FrameLayout.java:288)
E/AndroidRuntime(16052):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(16052):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
E/AndroidRuntime(16052):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
E/AndroidRuntime(16052):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
E/AndroidRuntime(16052):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(16052):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
E/AndroidRuntime(16052):    at android.view.View.layout(View.java:7035)
E/AndroidRuntime(16052):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
E/AndroidRuntime(16052):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/AndroidRuntime(16052):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(16052):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(16052):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(16052):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16052):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(16052):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(16052):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(16052):    at dalvik.system.NativeStart.main(Native Method)

因此,我如何能够动态改变层面?

问题回答

这里的问题是,当你确定布局时,家长的类型必须是哪一类,在这种情况下,就是一小块。 你们需要这样做:

ll.setLayoutParams(new FrameLayout.LayoutParams(30,60));

2. 完成以下任务的简单方式:

setContentView(R.id.main);    
LinearLayout layout= (LinearLayout) findViewById(R.id.left);
int width=60;
int height=60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
layout.setLayoutParams(parms);

如果你想在身高和宽度上进行筛选,然后使用以下代码:

setContentView(R.id.main);    
Display display = getWindowManager().getDefaultDisplay();
LinearLayout layout= (LinearLayout) findViewById(R.id.left);
int width=display.getWidth();
int height=display.getHeight();
LinearLayout.LayoutParams parms = new inearLayout.LayoutParams(width,height);
layout.setLayoutParams(parms);

希望能充分利用你们。

www.un.org/Depts/DGACM/index_spanish.htm 您可在您的功用类别中采用这一方法,以动态方式确定身高和宽度。

 public void setWidthAndHeight(Context context,RelativeLayout view,int width, int height) {
                width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, context.getResources().getDisplayMetrics());//used to convert you width integer value same as dp
                height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, context.getResources().getDisplayMetrics());
               //note : if your layout is LinearLayout then use LinearLayout.LayoutParam
                view.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
         }




相关问题
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 ...

热门标签