我正在尝试使用此外观制作应用enter image description here
但是以编程方式使我可以在运行时更改显示框的数量。 我试过以下,但没有成功 - 1)MainActivity.java
package com.bigx.dynamictesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.activity_relative);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for(int i = 0; i < 4; i++)
{
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(300 * getResources().getDisplayMetrics().density));
layoutParams.setMargins(0, 0, 0, (int)(20 * getResources().getDisplayMetrics().density));
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
textView.setText("TextView" + i);
linearLayout.addView(textView);
Button buttonA = new Button(this);
Button buttonB = new Button(this);
RelativeLayout.LayoutParams layoutParamsA = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_END, textView.getId());
RelativeLayout.LayoutParams layoutParamsB = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_LEFT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_START, textView.getId());
buttonA.setLayoutParams(layoutParamsA);
buttonA.setText(String.valueOf(i));
linearLayout.addView(buttonA);
buttonB.setLayoutParams(layoutParamsB);
buttonB.setText(String.valueOf(i));
linearLayout.addView(buttonB);
}
relativeLayout.addView(linearLayout);
}
2)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:fillViewport="false"
android:background="@color/colorAccent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bigx.dynamictesting">
</RelativeLayout>
</ScrollView>
产生此结果 - enter image description here
请建议我如何实现我的目标。 谢谢。 P.S.-抱歉我的英语不好。