[英]Android / Layout - Build pragmatically main activity layout
我正在學習如何以編程方式構建我需要開發的應用程序的布局。 我已經在xml文件中重現了正確的布局,現在我想以編程方式進行編輯(它將變為動態)。 我有一些疑問需要澄清。 因此,這是xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:padding="20sp"
android:background="@drawable/background"
tools:context=".DiLand" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@color/grayColor"
android:layout_marginTop="10sp" >
<TextView
android:text="1 copy"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="#000"
android:layout_centerHorizontal="true"
android:padding="25dp"/>
<TextView
android:text="$20"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:padding="25dp"/>
</RelativeLayout>
</LinearLayout>
這是我的Java代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_management);
//SCROLL VIEW
ScrollView scrollView = new ScrollView(this);
scrollView.setBackground(getResources().getDrawable(R.drawable.background));
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
scrollView.setPadding(20, 20, 20, 20);
//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//RELATIVE LAYOUT
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
//Need to understand how put a margin top to the relativeLayout
//TEXT VIEWS
TextView numberCopies = new TextView(this);
numberCopies.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
numberCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
numberCopies.setText("2 copies ");
RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
numberCopies.setLayoutParams(layoutParamsNumberCopies);
TextView priceCopies = new TextView(this);
priceCopies.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
priceCopies.setGravity(Gravity.CENTER);
numberCopies.setPadding(25, 25, 25, 25);
priceCopies.setTextColor(getResources().getColor(R.color.redColor));
priceCopies.setText("$ 25 ");
RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
priceCopies.setLayoutParams(layoutParamsPriceCopies);
scrollView.addView(scrollView);
scrollView.addView(linearLayout);
scrollView.addView(relativeLayout);
}
}
當我嘗試啟動活動時,程序崩潰並給我這種錯誤:-ClassCastException Android.widget.linearLayout $ LayoutParams無法轉換為android.widget.RelativeLayout $ LayoutParams
所以我需要了解我做錯了什么以及我該怎么做才能正確顯示布局。 這是我第一次嘗試這樣做,我在這里找到了一些關於stackoverflow的教程,這些教程可以幫助我更好地理解。 但是可能我缺少一些經驗。
謝謝
視圖的布局參數必須與其父視圖兼容。 在您的情況下,您在RelativeLayout
內有一個View
但您要為其提供LinearLayout.LayoutParams
,它為您提供ClassCastException
。
在您的代碼中LayoutParams
是不合格的,因此它歸結為您可能具有的導入
import android.widget.LinearLayout.LayoutParams;
處理LayoutParams
多種父布局時,最好只導入布局類
import android.widget.RelativeLayout;
然后在代碼中代替普通的LayoutParams
使用
RelativeLayout.LayoutParams
可以從其他LayoutParams
獲得它的資格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.