2011-06-16 65 views
0

我有自定义编码的布局来容纳所有的孩子。下面是代码:对齐自定义LinearLayout中的对象?

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中间,我的按钮放在最右边。考虑到这种层次观点,我该怎么做?

感谢

+1

我不明白为什么您没有XML文件中的布局?没有什么是你正在做的,你现在无法用XML做。 – dymmeh 2011-06-16 15:23:21

+0

我在选择中创建了我的布局和内部视图,其中有很多,我需要在运行时设置所有内容。此外,我还要以分层方式设置这些布局。我认为这会给我带来很大的安慰,编码 – ikbal 2011-06-16 15:30:38

回答

1

@dymmeh是完全正确的,你正在做的可能是你的main.xml如下每一件事情:

@Steve我又增加了TextView的建议,你会想选择要么TextViewOR

<?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> 

再经过ORTextView & ImageView之前您的java课程只需在您的onCreate之后拨打setContentView(R.layout.main);super.onCreate();

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

+0

对不起,我没有看到你的评论,你仍然可以做的一切都从XML开始,它会让事情变得更容易,你到底想要做什么? – 2011-06-16 15:40:59

+0

我的线性布局将是一个行的容器,它包含的内容将在运行时完全确定。我没有看到任何xml的好处。我无法解释我需要什么,但我可以告诉你,我需要写在代码中,谢谢 – ikbal 2011-06-17 06:30:29

+0

好吧,我会采取你的话,希望我可以帮助更多,但最后一件事,但如果唯一改变在运行时是内容而不是格式或布局,这仍然可以帮助您以编程方式完成所有这些工作,正如开发人员教程[ListView]中所做的那样(http://developer.android.com/resources/tutorials/ views/hello-listview.html)以获取每个列表项的格式。 – 2011-06-17 12:53:24

0

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