首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在自定义LinearLayout中对齐对象?

在自定义LinearLayout中对齐对象?
EN

Stack Overflow用户
提问于 2011-06-16 23:18:49
回答 2查看 802关注 0票数 0

我有定制的编码布局,以容纳所有的孩子。代码如下:

代码语言:javascript
复制
public abstract class ItemView extends LinearLayout{
    private ImageView icon;//cell icon
    public static final int iconID=12345;
    private Button accessorButton;//this will not be button ?
    public static final int accessorID=54321;



    public ItemView(Context context) {
        super(context);
    }

    public  void arrange(){

    }
    public  void setView(Context context){  
        int l=super.getLeft();
        int r=super.getRight();
        int t=super.getTop();
        int b=super.getBottom();
        icon=new ImageView(context);
        icon.setImageResource(R.drawable.star);
        addView(icon);
        accessorButton=new Button(context);
        accessorButton.setText("accessor");
        addView(accessorButton);
       // icon.layout(l+5, t+5, l+100, b-5);
        //accessorButton.layout(r-100, t+5, r-10, b-5);


    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);


    }


}

我有子布局,其中之一是:

代码语言:javascript
复制
public class ItemPlainView extends ItemView{

    private TextView text;

    public ItemPlainView(Context context) {
        super(context);
        setView(context);
    }
    public void setView(Context context){
      super.setView(context);
       text=new TextView(context);
       text.setText("plain view text");
       addView(text);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
    super.onLayout(changed, l, t, r, b);


    }
    public TextView getText() {
        return text;
    }
    public void setText(TextView text) {
        this.text = text;
    }

    @Override
    public void arrange() {
        // TODO Auto-generated method stub

    }
}

当我运行代码时,它自然会给我一个星形图标,一个按钮,然后是一个TextView。我需要替换对象并调整其大小。对于这个例子,我需要把星形图标放在最左边,TextView放在中间,我的按钮放在最右边。考虑到这种分层视图,我该如何做到这一点?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-16 23:39:42

@dymmeh是完全正确的,你正在做的每一件事都可能出现在你的main.xml中,如下所示:

@Steve我按照建议添加了另一个TextView,您可能希望选择OR之前的TextViewOR之后的TextView & ImageView

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:drawableLeft="@drawable/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <<--OR-->>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/star"
        android:weight="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="accessor"
        android:weight="1"
        />     
</LinearLayout>

那么您的java类将只需在super.onCreate();之后调用onCreate中的setContentView(R.layout.main);

比你所拥有的要容易得多。您也可以轻松地以任何您想要的方式排列这些项目。看LinearLayout,你也可以使用TableLayout,但我认为对于大多数情况来说,这太复杂了。

票数 1
EN

Stack Overflow用户

发布于 2011-06-16 23:42:01

使用XML布局会更容易做到这一点。我建议直接使用它。但是,如果您确实需要在代码中执行此操作,则可以将中间的布局设置为具有layout_weight = 1的TextView参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6374261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档