Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在自定义线性布局中对齐对象?_Android_Android Layout_Android Linearlayout_Custom View - Fatal编程技术网

Android 在自定义线性布局中对齐对象?

Android 在自定义线性布局中对齐对象?,android,android-layout,android-linearlayout,custom-view,Android,Android Layout,Android Linearlayout,Custom View,我有定制的编码布局来容纳所有的孩子。代码如下: 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 ac

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

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

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


谢谢

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

@Steve i根据建议添加了另一个
TextView
,您可以选择
之前的
TextView
,或
之后的
TextView

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


比你拥有的容易多了。您也可以按照自己的意愿轻松安排这些项目。请看,您也可以使用,但我认为对于大多数情况来说,这些都太复杂了。

使用XML布局更容易。我建议就用这个。但是,如果您确实需要在代码中执行此操作,可以将中间的TextView设置为具有布局权重为1的布局参数。

我不明白为什么您没有XML文件中的布局?现在在XML中没有做不到的事情。我创建了我的布局和内部视图,有很多,我需要在运行时设置所有内容。我还以分层的方式设置了这些布局。我想这会给我带来很多安慰。对不起,我没有看到你的评论,你仍然可以从xml开始做所有的事情,这会让事情变得更容易,你到底想做什么?我的线性布局将是一个行的容器,它包含的内容将在运行时完全确定。我看不出xml在这方面有什么好处。我不能确切地解释我需要什么,但我可以告诉你我需要用代码来写,谢谢,我会相信你的话,希望我能提供更多帮助,不过最后一件事是,如果在运行时唯一改变的是内容,而不是格式或布局,这仍然会为你省去编程的麻烦,正如开发人员教程中针对每个列表项的格式所做的那样。