Android以编程方式设计布局

问题描述:

enter image description hereAndroid以编程方式设计布局

如何以编程方式在android中进行此布局。

我只想一个想法不是整个编码的东西。

+0

为什么图像没有显示?你有没有人看到这个图像,这个问题与我一起? – Yawar

+0

我可以看到图像。但你应该多解释一下。哪些部分是布局元素,什么是静态图片?几乎所有事情都可以用'RelativeLayout' –

+0

实际上我不知道如何添加这些类型的参数,android:layout_alignBottom,android:layout_alignLeft等 – Yawar

这是答案,但不是直接的答案。这是我亲自做的以编程方式创建复杂的布局。

在XML中创建相同的布局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/some_big_image" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:background="#DD000000" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/imageView2" 
     android:layout_toRightOf="@+id/imageView2" 
     android:text="TextView" 
     android:textColor="@android:color/white" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView2" 
     android:layout_toRightOf="@+id/imageView2" 
     android:text="TextView" 
     android:textColor="@android:color/white" /> 
</RelativeLayout> 

现在从顶级父孩子

RelativeLayout mParent = new RelativeLayout(this); 
    RelativeLayout.LayoutParams mParentParams = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); 
    mParent.setLayoutParams(mParentParams); 

    ImageView mBigImageView = new ImageView(this); 
    mBigImageView.setLayoutParams(mParentParams); 
    mParent.addView(mBigImageView); 

正如你实践您可以直接代码,而无需创建XML一样。

基本思想:

  1. 创建RelativeLayout一种新的含有被对准底部

  2. 一个ImageView和两个TextView设置RelativeLayout的α来调整透明度

+0

其实我不知道如何添加这些类型的参数,android:layout_alignBottom,android:layout_alignLeft等 – Yawar

+1

@Yawar请看看这个链接:http://developer.android.com/guide/topics/ui/layout/relative.html 这将给你一些基本想法 – hungr

你可以有一个相对布局,其中包含一个大图像的图像视图在它的下面,一个更多的线性布局包含图像和文字作为“珍珠大陆...”和班轮布局的背景应该是50-60%透明。 使用形状为LL创建背景。并将其设置为对齐父级(相对)布局的底部。 希望这可以帮到你

+0

其实我不知道如何添加这些类型的参数,android:layout_alignBottom,android:layout_alignLeft等 – Yawar

+0

它更好地在xml中创建布局,然后在需要的地方膨胀。充气后设置各项所需的值 –

+0

另外还有一个样本提供了上面的 –