为了记录,一个长期的程序员,但对于android开发来说非常新。基本上是在尝试自学。我已经遇到了障碍,对我发现的事情进行了研究和尝试,但我确实陷入困境。
我想创建一行控件,当我点击一个按钮(Set)时,这是一个动态创建的控件。此行将添加到每个后续行的底部
Picture of what a row of controls would look like
我已经能够让程序向scrollview添加布局并实现动态添加行的目标,但是我无法正确地调整textview的大小。我已经尝试过layoutparam,setwidth没有运气。无论我做什么,文本都会被包装。
Picture of running app, rows added, TextView not sizing correctly
我尝试了一个linearlayout,但只是覆盖,甚至只是文本,textview保持相同的大小。
我试图让动态创建与我所做的“硬编码”布局相匹配,看看它的样子。具体来说,我错过了连续调整TextView和/或任何其他控件(视图对象?)的大小。
这是我的观点xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="@+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="@+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="@+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="@+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="@+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/label_time"
android:id="@+id/lblTime"
android:layout_row="0"
android:layout_column="1" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/label_Gait"
android:id="@+id/lblGait"
android:layout_row="0"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="@+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="@+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/my_root">
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我创建的原始xml,用于帮助我动态创建动画时想要的内容。
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:src="@drawable/ic_action_pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="bottom" />
<TextView
android:layout_width="232dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Canter - 99 min"
android:id="@+id/textView5"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:paddingLeft="15dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:text="Del"
android:id="@+id/button2"
android:layout_row="0"
android:layout_column="2" />
这是试图创建一个由图像,文本视图和按钮组成的行的函数(我一直在评论中显示我尝试过的内容)
@TargetApi(Build.VERSION_CODES.M)
private void buildDisplayLine(int count) {
// create a layout params to define how we add
// GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
// gridParams.width = GridLayout.LayoutParams.MATCH_PARENT;
// gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
// gridParams.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams imgLayout =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create a grid layout to add controls
GridLayout newGrid = new GridLayout(this);
newGrid.setId(100 + count);
newGrid.setColumnCount(3);
// LinearLayout newGrid = new LinearLayout(this);
// newGrid.setLayoutParams(imgLayout);
// newGrid.setVerticalGravity(LinearLayout.VERTICAL);
//create an image control
ImageView newImage = new ImageView(this);
newImage.setLayoutParams(imgLayout);
newImage.setImageResource(R.drawable.ic_action_pointer);
newImage.setId(count + 200);
Spinner spinner = (Spinner)findViewById(R.id.spinner_gaits);
TextView textGait = (TextView)spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView)findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
String display = gtResult + " - " + tmeResult + " min";
txtTime.setText("");
// create a text view control
TextView newText = new TextView(this);
newText.setLayoutParams(imgLayout);
newText.setGravity(Gravity.CENTER_VERTICAL);
Resources r = getResources();
int pc = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 332, r.getDisplayMetrics());
newText.setWidth(pc);
//newText.getLayoutParams().width = toPixels(432);
newText.setPadding(toPixels(15), 0, 0, 0);
// newText.setTextAppearance(android.R.style.TextAppearance_Large);
newText.setText(display);
newText.setId(300 + count);
// create a new button
Button newButton = new Button(this);
newButton.setLayoutParams(imgLayout);
newButton.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
newButton.getLayoutParams().width = toPixels(79);
newButton.setText("Del");
newButton.setId(400 + count);
// now add to the original grid layout
newGrid.addView(newImage);
newGrid.addView(newText);
newGrid.addView(newButton);
LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(newGrid);
spinner.requestFocus();
}
答案 0 :(得分:0)
为什么不使用LayoutInflater
来扩充XML,设置值并添加视图?像这样:
GridView newGrid = getLayoutInflater().inflate(R.layout.grid_row, null);
newGrid.setId(100 + count);
...
my_root.addView(newGrid);