Как заставить Margin-left (marginStart) изображение во фрагменте? - PullRequest
0 голосов
/ 24 апреля 2019

В моем приложении есть экран статистики / истории, который показывает историю активности пользователя за последние 7 дней.Я использовал 7 фрагментов на одном экране для представления каждого дня (7 дней назад, 6 дней назад ....).На каждом фрагменте есть изображение, которое должно быть изменено в зависимости от состояния.Проблема в том, что я не могу наложить на фрагменты изображения на левой стороне.Все они как-то сосредоточены.Мне нужна твоя помощь, чтобы решить ее

Это макет «History.java», который содержит frameLayouts

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_weight="3"
    android:background="#E0E1FF"
    tools:context=".controller.History">

    <FrameLayout
        android:id="@+id/frame_7_days_ago"
        android:layout_marginStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="85dp">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/frame_6_days_ago"
        android:layout_marginStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="85dp">

    </FrameLayout>  // ... below are all the same, 5 days ago, 4 etc...

Это один из макетов фрагментов (который похож наостальные из них).Первое изображение ImageView ниже (no_color) - это то, которое я хотел бы принудительно вложить в каждый фрагмент.Это изображение заменяется условием с помощью colorBar.setImageResource (R.drawable.green_color);Цвет хорошо меняется, но изображение всегда центрировано

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".controller.History"
    android:layout_marginStart="0dp">

<ImageView
    android:id="@+id/color_bar_image_7"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="0dp"
    android:src="@drawable/no_color"
    android:contentDescription="fragment_layout_7_days_ago" />

<RelativeLayout
    android:id="@+id/fragment7"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/fragment_7_days_ago_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="40dp"
        android:text="@string/_7_days_ago" />

    <ImageView
        android:id="@+id/fragment_7_days_ago_comment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="250dp"
        android:contentDescription="@string/view_comment_btn"
        android:onClick="comment7DaysAgo"
        android:src="@drawable/ic_comment_black_48px" />
</RelativeLayout>

</FrameLayout>

ОБНОВЛЕНИЕ: я забыл упомянуть, что каждый фрагмент вызывается для замены кадра того дня:

getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_7_days_ago, new StatisticSevenDaysAgo()).commit();

1 Ответ

0 голосов
/ 24 апреля 2019

Я не уверен, как именно должен выглядеть конечный результат, могут быть некоторые проблемы с относительной длиной и шириной match_paret.Но изменение структуры кадра на линейное расположение с горизонтальной ориентацией должно помочь при выравнивании изображения.

<LinearLayout
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:orientation="horizontal"
tools:context=".controller.History"
android:layout_marginStart="0dp">

<ImageView
    android:id="@+id/color_bar_image_7"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="0dp"
    android:src="@drawable/no_color"
    android:contentDescription="fragment_layout_7_days_ago" />

<RelativeLayout
    android:id="@+id/fragment7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/fragment_7_days_ago_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="40dp"
        android:text="@string/_7_days_ago" />

    <ImageView
        android:id="@+id/fragment_7_days_ago_comment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="250dp"
        android:contentDescription="@string/view_comment_btn"
        android:onClick="comment7DaysAgo"
        android:src="@drawable/ic_comment_black_48px" />
</RelativeLayout>

</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...