【问题标题】:How do I make an ImageButton rest at the bottom of a LinearLayout如何使 ImageButton 位于 LinearLayout 的底部
【发布时间】:2016-11-12 16:23:40
【问题描述】:

XML 文件

<include
    android:id="@id/my_toolbar"
    layout="@layout/toobar"
    />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/LinearEditTexts"
        >



        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Title_input"
            />

        <ImageButton
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_input_add"
            android:id="@+id/imageButton2"
            android:layout_width="50dp"
            android:layout_gravity="bottom"
            android:onClick="AddNewTextFields"


            />


    </LinearLayout>

</ScrollView>

我以编程方式制作 EdtTexts 的 Java 文件 列出所有edittexts = new ArrayList();

public void AddNewTextFields(View view) {
    TotalEdittexts++;
    if (TotalEdittexts > 100)
        return;
    EditText editText = new EditText(this);
    Containerlayout.addView(editText);
    editText.setGravity(Gravity.TOP);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
    layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
    layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

    editText.setLayoutParams(layoutParams);
    //if you want to identify the created editTexts, set a tag, like below
    editText.setTag("AddedEditText" + TotalEdittexts);

    alledittexts.add(editText);

}

每次onClickListener() 调用该函数时,我都试图让我的图像按钮位于由该图像按钮创建的EditText 下方。
设置它的重力并没有改变任何我想在这方面获得帮助的东西。

【问题讨论】:

    标签: android android-linearlayout android-scrollview android-imagebutton


    【解决方案1】:

    试试这个:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        androidd:orientation="vertical">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/linearlayout">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_rew"/>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      
      
       <include
          android:id="@id/my_toolbar"
          layout="@layout/toobar"
          />
      
      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="2">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:id="@+id/LinearEditTexts"
              >
      
      
      
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/Title_input"
                  />
      
      
          </LinearLayout>
      
      </ScrollView>
      <ImageButton
          android:layout_width="match_parent"
          android:src="@android:drawable/ic_media_rew"
          android:layout_height="wrap_content" />
      
      </LinearLayout>
      

      这是我的 XML 文件在编辑后的样子。
      感谢您的回答,它就像一个魅力

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 2011-03-01
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多