我在RelativeLayout中有一个linearLayout。我需要能够根据屏幕尺寸动态设置线性布局的高度。我有一些困难。 我怎么能做到这一点?在RelativeLayout中动态设置LinearLayout高度/宽度
MAIN:
public class MainActivity extends Activity {
LinearLayout L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
L=(LinearLayout)findViewById(R.id.top);
L.setLayoutParams(new LinearLayout.LayoutParams(1080, 400));
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:id="@+id/top"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
有什么难度/问题? –
@JustinJasmann我得到这个错误-java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams不能转换为android.widget.RelativeLayout $ LayoutParams at android.widget.RelativeLayout $ DependencyGraph.findRoots(RelativeLayout.java:1722) – Samantha