这是XML代码,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_grey" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<RelativeLayout
android:id="@+id/logolayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/layout_bg" >
<ImageView
android:id="@+id/company_logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/iv_bg"
android:scaleType="centerInside" />
<TextView
android:id="@+id/company_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@+id/company_logo"
android:gravity="center"
android:text="The D Corporation Inc."
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/sss_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/company_name"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_toRightOf="@+id/company_logo"
android:gravity="center"
android:text="RD Sales Management"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/login_user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sss_name"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_toRightOf="@+id/company_logo"
android:gravity="center"
android:singleLine="true"
android:textColor="#E55451"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/workorderlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/logolayout"
android:layout_marginTop="8dp"
android:background="@drawable/layout_bg" >
<TextView
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="12dp"
android:gravity="left"
android:text="Today’s Activity"
android:textSize="17dp"
android:textStyle="bold" />
<Button
android:id="@+id/id_linesp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/header_text"
android:layout_marginTop="8dp"
android:background="@color/light_grey" />
<HorizontalScrollView
android:layout_below="@+id/id_linesp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/lylat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:weightSum="3" >
<TableLayout
android:id="@+id/invoice_st"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/btn_bg"
android:paddingBottom="6dip" >
<TableRow>
.......仍然是代码流
我在类中使用了以下代码来设置Horizontall scrollview中的线性布局的宽度,
mLinearLayout = (LinearLayout) findViewById(R.id.lylat);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, HorizontalScrollView.LayoutParams.MATCH_PARENT);
mLinearLayout.setLayoutParams(params);
在此处设置宽度时,应用程序崩溃HorizontalScrollView.LayoutParams.MATCH_PARENT
它说java.lang.classcastexception linearLayout.layoutparams不能转换为framelayout.layoutparams,你的帮助将非常感谢
答案 0 :(得分:3)
可以分配视图的布局参数类型不依赖于视图的类,而是依赖于其容器的类。在这种情况下,容器是HorizontalScrollView
,需要FrameLayout.LayoutParameters
为孩子。如果要设置宽度和高度,请执行以下操作:
ViewGroup.LayoutParams params = mLinearLayout.getParams();
params.layout_width = metrics.widthPixels/4;
params.layout_height = ViewGroup.LayoutParams.MATCH_PARENT;
mLinearLayout.setLayoutParams(params);
换句话说,使用已由其父级分配给LayoutParams
的{{1}}。