RelativeLayout vs OtherLayout 排不上去

RelativeLayout vs OtherLayout can't get to line up

我正在创建一个应用程序,其中包含基于某些数据库数据的多个框。这些框需要显示特定的信息,当然是特定的格式。

以编程方式,我在过去几天尝试过...RelativeLayoutLinearLayoutGridLayout,甚至 TableLayout 都无法获得正确的结果。

这是我想要实现的布局....

这是我目前拥有的代码的基本部分...

        // Create Main Box for spacing
        RelativeLayout acctrl = new RelativeLayout(this);
        RelativeLayout.LayoutParams acctrlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 225);
        acctrlp.setMargins(15, 5, 15, 5);
        acctrl.setBackgroundResource(R.drawable.main_account_box);
        acctrl.setId(R.id.rlAcctId);
        acctrl.setClickable(true);
        acctrl.setOnClickListener(acctclick);
        acctrl.setOnLongClickListener(acctlongclick);

        // Account New Ball & Count
        RelativeLayout rlNew = new RelativeLayout(this);
        RelativeLayout.LayoutParams rlpNew = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        rlpNew.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlpNew.addRule(RelativeLayout.CENTER_IN_PARENT);
        rlNew.setId(R.id.rlNewId);

        ImageView ivNew = new ImageView(this);
        RelativeLayout.LayoutParams ivpNew = new RelativeLayout.LayoutParams(168,168);
        ivpNew.addRule(RelativeLayout.CENTER_HORIZONTAL);
        ivpNew.addRule(RelativeLayout.CENTER_IN_PARENT);
        ivNew.setBackgroundResource(R.drawable.acct_msg_ball_blue);
        rlNew.addView(ivNew,ivpNew);

        TextView tvNewCount = new TextView(this);
        RelativeLayout.LayoutParams tvpNewCount = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvpNewCount.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tvpNewCount.addRule(RelativeLayout.CENTER_VERTICAL);
        tvNewCount.setId(R.id.tvNewCountId);
        tvNewCount.setTextColor(COLOR.Black);
        tvNewCount.setPadding(0, -5, 0, 0);
        tvNewCount.setTextSize(25);
        tvNewCount.setText(String.valueOf(acct.get_new()));
        rlNew.addView(tvNewCount,tvpNewCount);

        TextView tvNewTitle = new TextView(this);
        RelativeLayout.LayoutParams tvpNewTitle = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvpNewTitle.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tvpNewTitle.addRule(RelativeLayout.BELOW,R.id.tvNewCountId);
        tvNewTitle.setTextColor(COLOR.Black);
        tvNewTitle.setTextSize(10);
        tvNewTitle.setPadding(0, 5, 0, 0);
        tvNewTitle.setText("new");
        rlNew.addView(tvNewTitle,tvpNewTitle);
        acctrl.addView(rlNew);

        // Account Name
        TextView tvName = new TextView(this);
        RelativeLayout.LayoutParams tvpName = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvpName.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        tvpName.addRule(RelativeLayout.RIGHT_OF,R.id.rlNewId);
        tvpName.addRule(RelativeLayout.LEFT_OF,R.id.tvCountId);
        tvName.setId(R.id.tvNameId);
        tvName.setTextColor(COLOR.Black);
        tvName.setPadding(5, 0, 5, 5);
        tvName.setTextSize(25);
        tvName.setText(acct.get_name());
        tvName.setEllipsize(TextUtils.TruncateAt.END);
        tvName.setSingleLine();
        acctrl.addView(tvName, tvpName);

        // Account Position
        TextView tvPos = new TextView(this);
        RelativeLayout.LayoutParams tvpPos = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvpPos.addRule(RelativeLayout.RIGHT_OF,R.id.rlNewId);
        tvpEmail.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        tvpPos.addRule(RelativeLayout.LEFT_OF,R.id.tvCountId);
        tvPos.setId(R.id.tvPosId);
        tvPos.setTextColor(COLOR.Black);
        tvEmail.setPadding(5, 5, 5, 0);
        tvEmail.setTextSize(15);
        tvEmail.setText(acct.get_pos());
        tvEmail.setEllipsize(TextUtils.TruncateAt.END);
        tvEmail.setSingleLine();
        acctrl.addView(tvPos, tvpPos);

不确定这是否是完成这一切的最佳方式。它对整个框使用 RelativeLayout。然后它使用另一个 RelativeLayout 来获得球,并且两个不同的高度 TextViews 在它的左边居中。我什至还没有开始 TOTAL JOBS 部分,但可能需要另一个内部 RelativeLayoutLinearLayout 来实现差异高度的居中 TextViews.

除了混乱的复杂性,目前的问题涉及到球...

1) 我无法让球在禁区内垂直居中...无论我尝试什么。此代码将其放在左上角。

2) 我不得不使用一些基于 CENTER_VERTICAL 的负填充 "fudge" 球内两个不同高度 TextViews 的居中。不确定这是否是实现它的最佳方法。

任何关于如何解决我的问题并理想地获得这些结果的建议都将不胜感激……我正在用这个来拔头发。就像我说的,我尝试了各种不同的布局命令,但似乎我越深入了解它们,我就遗漏了一些与定位、居中或叠加有关的关键元素。

谢谢...

有 xml 代码需要 UI 图片 SampleActivity.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;


public class SampleActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_layout);

        TextView textView = (TextView) findViewById(R.id.tvinitial);
        String newName = "New";
        SpannableString ss1 = new SpannableString(newName);
        ss1.setSpan(new RelativeSizeSpan(0.6f), 0, newName.length(), 0); // set size
        ss1.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newName.length(), 0);
        textView.setText("");
        textView.append("15");
        textView.append("\n");
        textView.append(ss1);
    }
}

可绘制XML circle.xml

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

    <item>
        <shape android:shape="rectangle">
            <corners android:radius="35dp" />

            <solid android:color="#4cc9df" />

            <stroke android:width="1dp" android:color="#4cc9df" />
        </shape>
    </item>
</selector>

round_corner.xml

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

    <item>
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />

            <solid android:color="@android:color/white" />

            <stroke android:width="1dp" android:color="@android:color/white" />
        </shape>
    </item>
</selector>

sample_layout.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:background="#e9e9e9"
              android:orientation="vertical"
              android:layout_gravity="center_vertical"
              android:padding="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@drawable/round_corner"
        android:padding="10dp">


        <ImageView
            android:id="@+id/arrow"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:src="@drawable/ic_loc_arrow"/>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@+id/arrow"
            android:layout_marginLeft="10dp">

            <TextView
                android:id="@+id/tvinitial"
                android:layout_width="65dp"
                android:layout_height="65dp"
                android:layout_centerVertical="true"
                android:background="@drawable/circle"
                android:fontFamily="sans-serif-medium"
                android:gravity="center"
                android:padding="5dp"
                android:text="@string/new_name"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold"/>


            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/tvinitial">

                <TextView
                    android:id="@+id/txtName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:fontFamily="sans-serif-condensed"
                    android:text="ADAM JUNE"
                    android:textColor="#474747"
                    android:textSize="25sp"
                    android:textStyle="normal"/>

                <TextView
                    android:id="@+id/second_txt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="25dp"
                    android:fontFamily="sans-serif-condensed"
                    android:text="Accountant"
                    android:textColor="#474747"
                    android:textSize="16sp"
                    android:textStyle="bold"/>
            </FrameLayout>

        </RelativeLayout>


        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@+id/arrow"
            android:layout_toLeftOf="@+id/right_layout">

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-condensed"
                    android:gravity="center"
                    android:text="TOTAL JOBS"
                    android:textColor="#3881a4"
                    android:textSize="14sp"
                    android:textStyle="bold"/>


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-condensed"
                    android:gravity="center"
                    android:text="125"
                    android:layout_marginTop="10dp"
                    android:textColor="#3881a4"
                    android:textSize="50sp"
                    android:textStyle="bold"/>
            </FrameLayout>

        </FrameLayout>

        <View
            android:id="@+id/right_layout"
            android:layout_width="25dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/arrow"/>

    </RelativeLayout>


</LinearLayout>

ic_loc_arrow.png

ic_loc_arrow.png

手机截屏

Mobile ScreenShot

希望这能解决您的问题。