【问题标题】:Android Layout programmatically以编程方式进行 Android 布局
【发布时间】:2014-05-02 12:56:44
【问题描述】:

布局\layout_main.xml

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

    <RelativeLayout
        android:id="@+id/rR2MessageBubble"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/drawable_sky_background_frame">

    </RelativeLayout>

</RelativeLayout>

布局\layout_component.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:orientation="vertical"
    android:background="@drawable/frame">


    <TextView
        android:id="@+id/txtViewTest"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="Hello!"
        android:gravity="center" />

</LinearLayout>

可绘制\frame.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <solid android:color="#FF07DD43" />

    <corners android:bottomRightRadius="3dp" 
                android:bottomLeftRadius="3dp" 
                android:topLeftRadius="3dp" 
                android:topRightRadius="3dp"/>

</shape>

MainActivity.java

![package com.kore.layoutdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author ayadav
 *
 */
public class MainActivity extends Activity {


 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_main);

    RelativeLayout rR2MessageBubble = (RelativeLayout)findViewById(R.id.rR2MessageBubble);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();

    //View-1
    View view1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);

    rR2MessageBubble.addView(view1);

    //View-2
    View view2 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);

    TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
    txtViewTest.setText("World");

    //params.addRule(RelativeLayout.RIGHT_OF, view1.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
    view2.setLayoutParams(params);

    rR2MessageBubble.addView(view2);

 }

}

输出

要求 我只想在 layout_main.xml 中添加 layout_component.xml 的多个实例,所以不要投反对票,请建议我通过我可以实现这一点的方式...谢谢

【问题讨论】:

  • 为什么要投反对票...我想在 layout_main.xml 中添加 layout_component.xml 的多个实例,所以不要投反对票,请建议我通过我可以实现这一点的方式...谢谢跨度>

标签: android layout programmatically-created


【解决方案1】:

你犯了一个小错误哥们,你给view2分配了一个错误的LayoutParam

替换,

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100); //or whatever width, height as per your need.

希望你理解你的错误。

【讨论】:

  • @@Breaker 非常感谢你......而不是否定投票你想出了解决方案:)
  • @@Breaker 很抱歉再次打扰你下面有同样的问题 params.addRule(RelativeLayout.BELOW, view1.getId());
  • view1.setId(8);事先也摆脱我:)
【解决方案2】:

可能是因为您将文本设置为“世界”。

TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
txtViewTest.setText("World");

【讨论】:

  • \@markieo 你的意思是说 view1 和 view2 指向同一个引用??
猜你喜欢
  • 2017-01-10
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多