@Boldy

Как прилепить два дочерних элемента TableRow к разным краям экрана?

Нужно, чтобы текст оставался слева, а кнопка лепилась справа, как float: right. Как это реализовать?

xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="kg.zuber.pocketlawyer.activities.MyLawsActivity">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"
                android:layout_marginRight="0dp"
                android:layout_marginLeft="0dp"
                android:layout_weight="0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>


java: (добавляет такие tableRow в onCreate)
for (int i = 0; i < savedLaws.size(); i++) {
                final Law law = savedLaws.get(i);
                TableRow tableRow = new TableRow(this);
                tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                TextView lawTitleLabel = new TextView(this);
                lawTitleLabel.setText(law.getTitle());
                tableRow.addView(lawTitleLabel);
                Button deleteButton = new Button(getApplicationContext());
                deleteButton.setBackgroundResource(R.drawable.button);
                deleteButton.setText("X");
                TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.MATCH_PARENT);
                tableRow.setGravity(Gravity.END);
                deleteButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dbm.deleteLaw(law.getId());
                        startActivity(new Intent(MyLawsActivity.this, MyLawsActivity.class));
                    }
                });
                tableRow.addView(deleteButton);
                tableLayout.addView(tableRow, i);
            }
  • Вопрос задан
  • 199 просмотров
Решения вопроса 1
Попробуй использовать android:layout_weight="1" вместе с android:layout_width="0dp" в TextView:

<TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView"/>


Программно вот так дожно сработать:
TextView lawTitleLabel = new TextView(this);
lawTitleLabel.setText(law.getTitle());
lawTitleLabel.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 0, 1f));
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@sputnic
Android Developer
android:gravity = right для кнопки
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы