我有一个带有edittext和一个按钮的活动,前一个具有属性
android:layout_weight="1"
当我按下按钮时,将以编程方式创建一个新的textview。我希望新的textview出现在edittext下。使用我拥有的代码,每个textview都在按钮的右侧创建。如何使它出现在新行的期望位置?我猜,由于textview是由代码创建的,所以唯一的方法是编程方式。
这是我的activity.xml
<LinearLayout 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:orientation="horizontal"
android:id="@+id/new_formulas"
>
<EditText
android:layout_weight="1"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/add_hint"
android:id="@+id/add_hint"
/>
<Button android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:id="@+id/add"
/>
</LinearLayout>
这是我的Java代码:
public class New_set extends AppCompatActivity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;
public static int integer = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_set);
mLayout = (LinearLayout) findViewById(R.id.new_formulas);
mEditText = (EditText) findViewById(R.id.add_hint);
mButton = (Button) findViewById(R.id.add);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText(mEditText.getText().toString());
}
private OnClickListener onClick() {
return new OnClickListener() {
@Override
public void onClick(View v) {
createNewTextView(mEditText.getText().toString());
}
};
}
private void createNewTextView(String text) {
RelativeLayout layout = new RelativeLayout(this);
final TextView textView = new TextView(this);
textView.setId(integer);
textView.setText(text);
RelativeLayout.LayoutParams params1 =new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (integer > 1)
params1.addRule(RelativeLayout.BELOW, integer - 1);
else
params1.addRule(RelativeLayout.BELOW, R.id.add_hint);
layout.setLayoutParams(params1);
mLayout.addView(textView);
integer +=1;
}
}
我尝试使用addRule和RelativeLayout做到这一点,但是它不起作用。
使用以下布局:
<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"
android:orientation="horizontal"
android:id="@+id/new_formulas"
>
<Button
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:id="@+id/add"
/>
<EditText
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/add_hint"
android:id="@+id/add_hint"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/add"
/>
<LinearLayout
android:id="@+id/hints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add_hint"
android:orientation="vertical"/>
</RelativeLayout>
现在,每个孩子TextView
都可以hints
LinearLayout
像上面一样添加到孩子中,而无需使用规则。
重要说明:使用时,请LayoutParams
始终从父布局使用它。例如这里应该是LinearLayout.LayoutParams
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句