開発する必要があるアプリのレイアウトをプログラムで構築する方法を学んでいます。私は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);
}
}
アクティビティを起動しようとすると、プログラムがクラッシュし、次のようなエラーが表示されます:
だから、私が間違っていることと、レイアウトを正しく表示するために何ができるかを理解する必要があります。これをやろうとするのは初めてで、理解を深めるのに役立つスタックオーバーフローに関するチュートリアルをいくつか見つけました。しかし、おそらく私はいくつかの経験を逃しています。
ありがとう