2014-10-08 73 views
0

摘要如何使用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的字体大小。

enter image description here


尝试

为了在应用程序的底部来定位hello,可能的方法的列表进行检查和选择textView.setBottom(TRIM_MEMORY_BACKGROUND);。注意:除TRIM_MEMORY_BACKGROUND之外的其他常量可以指定,但为了检查setBottom的输出,选择了一个随机常量。

然而,这是不可能的运行在下面的错误的应用程序作为setBottom结果:

enter image description here

一旦@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

你试过'安卓重力(底部| CENTER_HORIZONTAL)'上你的TextView?这似乎是直接从Javadoc – Ordous 2014-10-08 16:14:57

回答

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); 
+0

直接解决方案感谢您发布答案,但问题仍然存在。 – 030 2014-10-08 15:53:53

+0

这工作(+1)。谢谢。 – 030 2014-10-08 16:21:48

1

一个的Android View是通过修改其layout parameters定位在ViewGroup内。

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); 
    }