摘要如何使用Java在Android中更改TextViews的位置?
添加android:layout_alignParentBottom="true"
到位于activity_main.xml中位置的TextView在Android应用底部的TextView元素。
也可以改变使用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);
}
}
你试过'安卓重力(底部| CENTER_HORIZONTAL)'上你的TextView?这似乎是直接从Javadoc – Ordous 2014-10-08 16:14:57