80,359
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试一下"
android:id="@+id/TextView_Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_below="@id/TextView_Title"
android:onClick="e_Button_Clicked"
android:id="@+id/Button_Add"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Button_Add"
android:id="@+id/LinearLayout_Test"
android:orientation="vertical"
>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/LinearLayout_Test"
android:text="我是有底线的"
/>
</RelativeLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayoutTest=findViewById(R.id.LinearLayout_Test);
}
private void f_add(){
mLinearLayoutTest.removeAllViews();
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,300
);
layoutParams.gravity=Gravity.CENTER_VERTICAL;
TextView textView=new TextView(this);
textView.setText("测试垂直居中的文字");
textView.setLayoutParams(layoutParams);
mLinearLayoutTest.addView(textView);
}
public void e_Button_Clicked(View view){
int iViewID=view.getId();
switch (iViewID){
case R.id.Button_Add:
f_add();
break;
}
}
private LinearLayout mLinearLayoutTest;
}