簡體   English   中英

如何使用Java在Android中更改TextViews的位置?

[英]How to change the position of TextViews in Android using Java?

摘要

向位於activity_main.xml中的T​​extView元素添加android:layout_alignParentBottom="true" 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不會放在底部。

根據另一個問答 ,以下代碼

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

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

通過修改其布局參數,可將Android View置於ViewGroup

ViewGroup每個子類都有一個布局參數的對應子類,必須使用該子類來使用該視圖組的功能實現精細控制。

在這種情況下,如果要使用RelativeLayout的功能來布局視圖,則需要在每個子視圖上使用RelativeLayout作為視圖組,並使用RelativeLayout.LayoutParams作為布局參數。

原始代碼將布局參數用於相對布局,但是您的視圖組是LinearLayout 這不能按預期工作,但不會觸發編譯時錯誤或類setLayoutParams異常,因為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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM