創建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);
}
}
你加試驗場? – SMA 2015-03-02 09:25:29
將其添加到您的主佈局中。 – 2015-03-02 09:28:10
代碼是正確的,它的工作原理你把這個代碼放在哪裏? – 2015-03-02 09:28:15