摘要
将android:layout_alignParentBottom="true"
添加到位于activity_main.xml中的TextView元素,将textview定位在Android应用程序的底部。
也可以使用Java更改Android中的TextView位置,例如:
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
更改hello
的字体大小。
尝试
为了将hello
放在应用程序的底部,检查了可能的方法列表,并选择了textView.setBottom(TRIM_MEMORY_BACKGROUND);
。注意:可以指定除TRIM_MEMORY_BACKGROUND
之外的其他常量,但为了检查setBottom的输出,选择了随机常量。
但是,无法运行应用程序,因为setBottom会导致以下错误:
添加@SuppressLint("NewApi")
后,可以运行应用程序,但hello
不会放在底部。
根据another Q&A以下代码
RelativeLayout.LayoutParams params1 = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
应该可以更改hello
的位置,但这也不起作用。
代码
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
// @SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
// textView.;
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
// textView.setBottom(TRIM_MEMORY_BACKGROUND);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
textView2.setText("world");
ll.addView(textView);
ll.addView(textView2);
setContentView(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
RelativeLayout ll = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
TextView textView = new TextView(this);
// textView.;
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
textView.setLayoutParams(params1);
// textView.setBottom(TRIM_MEMORY_BACKGROUND);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
ll.addView(textView);
setContentView(ll);
答案 1 :(得分:2)
Android View
位于ViewGroup
内,可修改其layout parameters。
ViewGroup
的每个子类都有一个相应的布局参数子类,必须使用该子类来使用该视图组的功能实现精细控制。
在这种情况下,如果您想使用RelativeLayout
的功能布置视图,则需要使用RelativeLayout
作为视图组,并使用RelativeLayout.LayoutParams
作为布局参数每个孩子的观点。
原始代码使用相对布局的布局参数,但您的视图组是LinearLayout
。这不能按预期工作,但它不会触发编译时错误或类转换异常,因为setLayoutParams
调用将所有布局参数类的父类作为参数。
我运行了如下修改的测试代码(只需使用与布局管理器一致的视图组),它解决了问题:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout ll = new RelativeLayout(this);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
textView2.setText("world");
ll.addView(textView);
ll.addView(textView2);
setContentView(ll);
}