小编Joi*_*dio的帖子

自定义View中RelativeLayout内的Textview没有正确应用重力?

所以我有一个设置,我正在创建自己的视图,我正在添加一些TextViews.但是,重力设置被打破了.(它水平居中,但不是垂直居中)我这样做是因为除了TextView之外我还在其他视图中绘制其他东西,但这些工作正常.TextView引力只有一个问题.这是我所拥有的部分代码.

public class myView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public myView (Context context) {
        super(context);
        setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        textView1 = new TextView(context);
        // initialize textView1 string, id, textsize, and color here
        textView2 = new TextView(context);
        // initialize textView2 string, id, textsize, and color here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Resources res = getResources();
        // calculate out size and position of both …
Run Code Online (Sandbox Code Playgroud)

android view gravity textview

6
推荐指数
1
解决办法
5230
查看次数

自定义视图 - 如何设置ID以便通过findViewById()找到它们?

所以我在代码中设置了一个自定义视图(没有为它定义XML布局),我想知道如何正确定义子视图ID.我DO有定义ID的类似于这样的XML文件.但我所理解的吧,至少,因为我没有一个XML布局没有AttribSet传递..所以我的所有构造函数是刚(上下文的背景下)的类型.

<resources>
    <item type="id" name="textview1"/>
    <item type="id" name="textview2"/>
</resources>
Run Code Online (Sandbox Code Playgroud)

我的观点看起来像这样:

public class MyView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public MyView(Context context) {
        super(context);
        LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        setLayoutParams(fill);

        Resources res = getResources();

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(fill);

        textView1 = new TextView(context);
        textView1.setId(R.id.textview1);
        // other init stuff here

        textView2 = new TextView(context);
        textView2.setId(R.id.textview2);
        // other init stuff here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // *snip …
Run Code Online (Sandbox Code Playgroud)

null android view

5
推荐指数
1
解决办法
4946
查看次数

标签 统计

android ×2

view ×2

gravity ×1

null ×1

textview ×1