当我尝试通过代码设置边距时,我的应用程序崩溃了。
<LinearLayout
android:id="@+id/lay_AC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/btn_f_ac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/accesories_icon"
android:minWidth="48dp"
android:textColor="#151551" />
<TextView
android:id="@+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accesories"
android:textColor="#f82229"
android:textSize="12sp" />
</LinearLayout>
代码:
LinearLayout lay_AC;
lay_AC = (LinearLayout) findViewById(R.id.lay_f_vd);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin = 300;
lay_AC.setLayoutParams(lp);
进口:
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
错误:
07-03 14:48:22.592: W/dalvikvm(27674): threadid=1: thread exiting with uncaught exception (group=0x40a431f8)
07-03 14:48:22.602: E/AndroidRuntime(27674): FATAL EXCEPTION: main
07-03 14:48:22.602: E/AndroidRuntime(27674): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.View.measure(View.java:12728)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.View.measure(View.java:12728)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.View.measure(View.java:12728)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-03 14:48:22.602: E/AndroidRuntime(27674): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.View.measure(View.java:12728)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.os.Looper.loop(Looper.java:137)
07-03 14:48:22.602: E/AndroidRuntime(27674): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-03 14:48:22.602: E/AndroidRuntime(27674): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 14:48:22.602: E/AndroidRuntime(27674): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 14:48:22.602: E/AndroidRuntime(27674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-03 14:48:22.602: E/AndroidRuntime(27674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-03 14:48:22.602: E/AndroidRuntime(27674): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
首先,您为LinearLayout
传递了错误的ID。
将行lay_AC = (LinearLayout) findViewById(R.id.lay_f_vd);
替换为
lay_AC = (LinearLayout) findViewById(R.id.lay_AC);
您的导入也存在问题。 尝试导入
import android.widget.LinearLayout.LayoutParams;
而不是
import android.widget.RelativeLayout.LayoutParams;
答案 1 :(得分:1)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
使用上面的代码设置保证金
答案 2 :(得分:0)
从
更改导入声明 import android.widget.RelativeLayout.LayoutParams;
到
import android.view.ViewGroup.LayoutParams;
并更改linearlayout id
R.id.lay_f_vd
到
R.id.lay_AC
答案 3 :(得分:0)
你不能直接使用它;
lp.leftMargin = 300;
lay_AC.setLayoutParams(lp);
你需要为你的LinaerLayout提供一些包装:
<LinearLayout >
<!-- Put your linear layout which you want to set margin or padding-->
<LinearLayout>