Android:为什么没有显示TextField?

时间:2015-03-02 09:24:19

标签: java android textview jtextfield

    TextView t = new TextView(this);
    t.setX(0);
    t.setY(0);
    t.setText("Tap");
    t.setTextColor(Color.GREEN);
    t.setTextSize(25);
    t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
    setContentView(t);

我在ActionBarActivity中,为什么我使用“this”作为上下文,无法理解为什么textField没有显示,这里有什么问题?

2 个答案:

答案 0 :(得分:1)

试试这个

    TextView t= new TextView(this);
    t.setX(0);
    t.setY(0);
    t.setText("Tap");
    t.setTextColor(Color.GREEN);
    t.setTextSize(25);
    addContentView(t, new FrameLayout.LayoutParams(100, 100));

答案 1 :(得分:0)

创建xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="in.appkart.flashlight.MainActivity" >

</FrameLayout>

和MainActivity

package in.appkart.flashlight;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View frameLayout = findViewById(R.id.frame_layout);

        TextView t = new TextView(this);

        t.setX(0);
        t.setY(0);
        t.setText("Tap");
        t.setTextColor(Color.GREEN);
        t.setTextSize(25);
        t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
        t.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        ((FrameLayout) frameLayout).addView(t);
    }
}