簡體   English   中英

如何在Android代碼中創建此布局?

[英]How to create this layout in Android code?

更新:問題已解決。 我的真實代碼中有一個小錯誤。 下面的代碼工作正常。 非常感謝您的支持!

================================================== ================================我有一個線性布局和一些類似下面的文本視圖。 線性布局為水平方向。 下面的布局可以根據需要工作,但是我想向其動態添加TextView,因此我想用代碼而不是xml對其進行初始化。 但是,當我在代碼中執行此操作時,文本視圖將顯示在線性布局的頂部,而不是我想要的底部。 xml布局文件為:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:baselineAligned="false">
      <TextView 
           android:text="dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg"
           android:layout_weight="1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="#eeffee"
           android:layout_gravity="bottom"/>

      <TextView 
           android:text="asd asd asd asd asd asd asd asd asd asd"
           android:layout_weight="1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="#eeeeff"
           android:layout_gravity="bottom"/>


      <TextView 
           android:text="qweoiu qweoiuqwe oiqwe qwoeiu qweoiu qweoiuq weoiuqw eoiquw eoiqwue oqiweu qowieu qowieu qoiweu qowieu qowieu qowieu qowieu qoiweu qowieu qoiwue "
           android:layout_weight="1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="#ffeeee"
           android:layout_gravity="bottom"/>

 </LinearLayout>

我的代碼是:

    LinearLayout container = new LinearLayout(getContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    container.setLayoutParams(layoutParams);
    container.setOrientation(LinearLayout.HORIZONTAL);
    container.setBaselineAligned(false);

    TextView textView = new TextView(getContext());
    textView.setText("asgfg fgf");
    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
    layoutParams1.gravity = Gravity.BOTTOM;
    textView.setLayoutParams(layoutParams1);
    container.addView(textView);

    TextView textView2 = new TextView(getContext());
    textView2.setText("asgfg fgf");
    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
    layoutParams2.gravity = Gravity.BOTTOM;
    textView2.setLayoutParams(layoutParams2);
    container.addView(textView2);

    TextView textView3 = new TextView(getContext());
    textView3.setText("asgfg fgf");
    LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
    layoutParams3.gravity = Gravity.BOTTOM;
    textView3.setLayoutParams(layoutParams3);
    container.addView(textView3);

我的代碼不起作用,“文本視圖”顯示在頂部而不是底部。 請幫助我看看我在做什么錯。 謝謝。

為TextView設置重力僅說明應放置其文本的位置-而不是視圖。 您必須使用layout_gravity或版圖的重力參數(父級的重力或LayoutParams的重力參數),但不能使用TextView的重力。

已編輯

((LinearLayout.LayoutParams)myTextView.getLayoutParams()).gravity = Gravity.BOTTOM;

再次編輯

LinearLayout container = new LinearLayout(getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.BOTTOM;
container.setLayoutParams(layoutParams);

如果在onCreate中,您正在使用

setContentView(R.layout.your_xml);

比您必須配合Deepzz回答。 或者你可以使用

setContentView(container);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM