【问题标题】:Aligning objects in custom LinearLayout?在自定义 LinearLayout 中对齐对象?
【发布时间】:2011-06-16 15:18:49
【问题描述】:

我有自定义编码布局来容纳所有孩子。代码如下:

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);


    }


}

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

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 中,并将我的按钮放在最右侧。考虑到这种分层视图,我该怎么做?

谢谢

【问题讨论】:

  • 我不明白您为什么没有 XML 文件中的布局?没有什么是你现在不能在 XML 中做的。
  • 我根据选择创建布局和内部视图,它们有很多,我需要在运行时设置所有内容。我还必须以分层方式设置这些布局。我认为它会给我带来很多舒适的编码

标签: android android-layout android-linearlayout custom-view


【解决方案1】:

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

@Steve 我按照建议添加了另一个TextView,您可能希望在OR 之前选择TextView,或者在OR 之后选择TextViewImageView

<?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类只需要在onCreate之后调用setContentView(R.layout.main);super.onCreate();

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

【讨论】:

  • 对不起,我没有看到你的评论,你仍然可以从 xml 开始,它会让事情变得更容易,你到底想做什么?
  • 我的线性布局将是一行的容器,它包含的内容将在运行时完全确定。我看不出xml有什么好处。我无法准确解释我需要什么,但我可以告诉你我需要用代码编写它,谢谢
  • 好吧,我相信你的话,希望我能提供更多帮助,但最后一件事,如果在运行时唯一改变的是内容而不是格式或布局,这仍然会为你节省以编程方式完成所有这些操作的麻烦,就像在开发人员教程ListView 中针对每个列表项的格式所做的那样。
【解决方案2】:

使用 XML 布局要容易得多。我建议只使用它。但是,如果你真的需要在代码中这样做,你可以将中间的 TextView 设置为具有 layout_weight = 1 的布局参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 2023-03-18
    • 2015-12-16
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多