在代码中写view 的长宽高等

获得资源的id的另一种方法

int layoutRes = getResources().getIdentifier("pager_view" + i, "layout", getPackageName());

这里写图片描述

假设我们想在这个相对布局中填一个子节点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</RelativeLayout>

这里写图片描述

package com.example.com.fmyh;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        RelativeLayout rl=(RelativeLayout) findViewById(R.id.rl);

        TextView tv = new TextView(this);

        tv.setText("你好");

        //设置长宽高 这里要选父节点RelativeLayout的包
        LayoutParams lf = new LayoutParams(20,20);
        //设置左右上下的margin值
        lf.setMargins(10, 10, 10, 10);

        tv.setLayoutParams(lf);

        rl.addView(tv);
    }


}

posted on 2016-09-19 20:29  木鱼哥  阅读(178)  评论(0编辑  收藏  举报

导航